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.
Solution to Sleeping-barber Problem:
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/