Sleeping barber problem
In computer science, the sleeping barber problem is a classic inter-process communication and synchronization problem that illustrates the complexities that arise when there are multiple operating system processes.
The problem was originally proposed in 1965 by computer science pioneer Edsger Dijkstra, who used it to make the point that general semaphores are often superfluous.
Problem statement
Imagine a hypothetical barbershop with one barber, one barber chair, and a waiting room with n chairs 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 the barber is working, the customer leaves if all chairs are occupied and sits in an empty chair if it's available
- 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
A multiple sleeping barbers problem has the additional complexity of coordinating several barbers among the waiting customers.
Solutions
There are several possible solutions, but all solutions require a mutex, which ensures that only one of the participants can change state at once. The barber must acquire the room status mutex before checking for customers and release it when they begin either to sleep or cut hair; a customer must acquire it before entering the shop and release it once they are sitting in a waiting room or barber chair, and also when they leave the shop because no seats were available. This would take care of both of the problems mentioned above. A number of semaphores is also required to indicate the state of the system. For example, one might store the number of people in the waiting room.Implementation
The following pseudocode guarantees synchronization between barber and customer and is deadlock free, but may lead to starvation of a customer. The problem of starvation can be solved with a first-in first-out (FIFO) queue. The semaphore would provide two functions:wait and signal, which in terms of C code would correspond to P and V, respectively.- The first two are mutexes
Semaphore accessWRSeats = 1 # if 1, the number of seats in the waiting room can be incremented or decremented
Semaphore custReady = 0 # the number of customers currently in the waiting room, ready to be served
int numberOfFreeWRSeats = N # total number of seats in the waiting room
def Barber:
while true: # Run in an infinite loop.
wait # Try to acquire a customer - if none is available, go to sleep.
wait # Awake - try to get access to modify # of available seats, otherwise sleep.
numberOfFreeWRSeats += 1 # One waiting room chair becomes free.
signal # I am ready to cut.
signal # Don't need the lock on the chairs anymore.
#
def Customer:
while true: # Run in an infinite loop to simulate multiple customers.
wait # Try to get access to the waiting room chairs.
if numberOfFreeWRSeats > 0: # If there are any free seats:
numberOfFreeWRSeats -= 1 # sit down in a chair
signal # notify the barber, who's waiting until there is a customer
signal # don't need to lock the chairs anymore
wait # wait until the barber is ready
#
else: # otherwise, there are no free seats; tough luck --
signal # but don't forget to release the lock on the seats!
#