2

Sleeping Barber Problem in Operating Systems

 1 year ago
source link: https://www.geeksforgeeks.org/videos/sleeping-barber-problem-in-operating-systems/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sleeping Barber Problem in Operating Systems

Sleeping Barber Problem in Operating Systems
Hello everyone, welcome to Geeks for geeks,

In this video, we have covered what is sleeping barber problem in operating systems. The sleeping barber problem is a classical process synchronization problem. 

There is a barber-shop having a single barber, one barber chair, and n chairs for the waiting customers.

  • If there is no customer, then the barber sits and sleeps on his own chair.
  • When a customer enters, he wakes up the barber.
  • If the barber is cutting a customer’s hair and more customers enter the barber shop, then these customers wait for their turn only if there are empty chairs. Otherwise, they leave the barber shop if there are no empty chairs for them to sit and wait.
  • Customers cannot stand and wait.

Solution to Sleeping-barber Problem:

  • The solution to the Sleeping Barber problem uses 3 semaphores.
  • The first semaphore counts the number of customers already waiting in the barber-shop when a new customer arrives. This count does not include the customer sitting in the barber chair because he is not waiting.
  • The second semaphore is a binary semaphore which indicates if the barber is free or busy working.
  • The third is a mutex lock which provides mutual exclusion in the entire scenario.

In the solution, if the value of the first semaphore is equal to the number of chairs in the barber shop (except the barber chair), the newly arrived customer leaves the barber shop immediately. When the barber comes to his shop in the morning, he executes the procedure barber(), which causes him to block on the semaphore customers because its initial value is 0. So the barber goes to sleep in his chair until the first customer arrives and wakes him up. When a customer arrives, he executes customer() procedure to acquire the mutex for entering the critical region. In case, another customer enters thereafter, the second customer will not be able to do anything unless the first customer has released the mutex.

The customer then counts the number of empty chairs in the barber-shop. If the number of waiting customers is less than the number of chairs, then the customer occupies one of the empty chairs. Otherwise, he leaves by releasing the mutex. If a chair is empty, then the customer waits in the barber shop and increments the value of the variable waiting. Further, he/she also increments the semaphore customer. This cause the barber to wake up in case he is asleep. Now, both the customer and barber are awake and the barber is ready to give the customer a haircut. After the haircut is done, the customer exits the procedure. Finally, if there are no waiting customers, the barber sleeps in the barber chair.

Sleeping barber problem in OS: https://www.geeksforgeeks.org/sleeping-barber-problem-in-process-synchronization/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK