8000 GitHub - joaoipiraja/TheATMProblem: Inspired by the classic semaphores synchronization problem: Sleeping Barber
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

joaoipiraja/TheATMProblem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

The ATM Problem

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

Analysis

Inspired by the classic semaphores synchronization problem

Sleeping Barber com m customers and n barbers

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

Pseudocode


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)
            }
        }
    }
    
}




Solution

VIDEO DEMO

IOS particularities

DispatchQueue.global

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.

DispatchQueue.main

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{ 

}

DispatchSemaphore

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()

Resources

About

Inspired by the classic semaphores synchronization problem: Sleeping Barber

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0