29 The barbershop problem
The original barbershop problem was proposed by Dijkstra. A variation of it appears in Silberschatz and Galvin’s Operating Systems Concepts (Silberschatz and Galvin 1997).
A barbershop consists of a waiting room with \(n\) chairs, and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.
To make the problem a little more concrete, I added the following information:
Customer threads should invoke a function named
getHairCut.If a customer thread arrives when the shop is full, it can invoke
balk, which does not return.The barber thread should invoke
cutHair.When the barber invokes
cutHairthere should be exactly one thread invokinggetHairCutconcurrently.
Write a solution that guarantees these constraints.
29.1 Barbershop hint
n is the total number of customers that can be in the shop: three in the waiting room and one in the chair.
customers counts the number of customers in the shop; it is protected by mutex.
The barber waits on customer until a customer enters the shop, then the customer waits on barber until the barber signals him to take a seat.
After the haircut, the customer signals customerDone and waits on barberDone.
29.2 Barbershop solution
This solution combines a scoreboard and two rendezvouses1. Here is the code for customers:
If there are \(n\) customers in the shop any customers that arrive immediately invoke balk. Otherwise each customer signals customer and waits on barber.
Here is the code for the barber:
Each time a customer signals, the barber wakes, signals barber, and gives one hair cut. If another customer arrives while the barber is busy, then on the next iteration the barber will pass the customer semaphore without sleeping.
The names for customer and barber are based on the naming convention for a rendezvous, so customer.wait() means “wait for a customer,” not “customers wait here.”
The second rendezvous, using customerDone and barberDone, ensures that the hair cut is done before the barber loops around to let the next customer into the critical section.
This solution is in sync_code/barber.py.
The plural of rendezvous is rare, and not all dictionaries agree about what it is. Another possibility is that the plural is also spelled “rendezvous,” but the final “s” is pronounced.↩︎