Q1. Explain the Sleeping Barber Problem.
Ans. The barber shop has one barber, one barber chair and ‘n’ number of chairs for waiting customers, if any one set on. If there are no customers present, the barber set down on the barber chair and sleep. When a customer arises he has to wake-up the sleeping barber. If additional customer arrives while the barber is cutting a customer’s hair, they either sit down or leave the shop. The problem is to program the barber and the customer without getting into race condition.
It uses three semaphore, they are:
This counts waiting customers.
The number of barber (0 or 1).
This is used for mutual exclusion.
We also need a variable ‘waiting’, which also counts the waiting customers. It is essentially a copy of customers; the reason for having this variable is that, there is no way to read the current value of semaphore. In this solution, a customer entering the shop has to count number of waiting customers, if it is less than the number of chairs, he stays otherwise he leaves.