Project for N1 - Operatinal Systems Course taught by Prof.Dr.Fernando Parente Garcia - Computer Engineering 2nd year - Federal Institute of Science & Technology of Ceará - Fortaleza Campus - 2022.2
Inspired by the classic semaphores synchronization problem
Imagine a hypothetical barbershop with m barber chair and a waiting room with n chairs (n may be 0) for waiting customers. The following rules apply:
- If there are no customers, the barber falls asleep in the chair
- A customer must wake the barber if he is asleep
- If a customer arrives while all barbers is working, the costumers waits til one is avaible
- When the barber finishes a haircut, he inspects the waiting room to see if there are any waiting customers and falls asleep if there are none
Barbers Thread Queue - fb
Customers Thread Queue - fc
Semaphore customers = 0
Semaphore barber = 0
Semaphore mutex = 1
class Barber {
init{
fb.enqueue(self)
}
func run(){
loop infinito{
down(customer)
down(mutex)
client = waitingQueues.dequeue() //pick a client
up(barber)
up(mutex)
// wait for the client to finish
down(client.cutting)
}
}
}
class Customer{
semaforo cutting = 1
func run(){
loop infinito{
down(mutex);
if(fc.size < fb.disponiveis.size){ /
fc.enqueue(self)
up(customers)
up(mutex)
down(barber)
//realize the action
up(cutting) //send a signal to barber to finish
}else{
up(mutex)
}
}
}
}
Returns the global system queue with the specified quality-of-service class.
DispatchQueue.global(qos:).async{
}
DispatchQueue.global(qos:).asyncAfter(deadline:){ //execute after a delay
}
- UserInteractive: System will give more resources for this setting, it should be used for the task which relates to UI, animation, etc.
- UserInitialed: This setting is for some time you want to prevent the user from actively using your app.
- Default: Have lower priority than UserInteractive and UserInitialed. if you have not set any QoS, then QoS will be this case.
- Utility: For some task that doesn’t need user to track.
- Background: When you need to maintain or clean up the task you create.
The dispatch queue associated with the main thread of the current process. Used to updates the view content in background
/*
function returns control to the current queue only after the task is finished. It blocks the queue and waits until the task is finished.
*/
DispatchQueue.main.async{
}
/*
Function returns control to the current queue right after task has been sent to be performed on the different queue. It doesn't wait until the task is finished. It doesn't block the queue.
*/
DispatchQueue.main.sync{
}
An object that controls access to a resource across multiple execution contexts through use of a traditional counting semaphore.
let semaphore = DispatchSemaphore(value: 3)
semaphore.wait()
semaphore.signal()
- Sleeping barber problem on Wikipedia
- http://lasdpc.icmc.usp.br/
- http://www.cs.csi.cuny.edu/
- The Beauty of Semaphores in Swift by Roy Kronenfeld
- DispatchSemaphore by Apple Developer Documentation
- iOS-Introducing DispatchQueue in Swift by Gordon Feng
- Difference between DispatchQueue.main.async and DispatchQueue.main.sync? on Stackoverflow