Barbershop Problem Simulation
Barbershop Problem Simulation
Goals: This laboratory exercise provides practice process
coordination using semaphores.
Sleeping Barber Problem: Many books, including our textbook, state
variations of the Sleeping Barber Problem, which was first proposed by
Edsger W. Dijkstra in 1968. (E. W. Dijkstra, "Co-operating Sequential
Processes", in F. Genuys (ed.), Programming Languages, Academic
Press, 1968, pp. 43-112.) This lab considers the following version:
Three barbers work independently in a barber shop:
-
The barbershop has 3 barber chairs, each of which is assigned to one
barber.
-
Each barber follows the same work plan:
-
The barber sleeps (or daydreams) when no customer
customer is waiting (and is not in the barber's own chair).
-
When the barber is asleep, the barber waits to be awaken by a new customer.
(A sign in the shop indicates which barber has been asleep longest, so the
customer will know which barber to wake up if multiple barbers are asleep.)
-
Once awake, the barber cuts the hair of a customer in the barber's chair.
-
When the haircut is done, the customer pays the barber and then is free to
leave.
-
After receiving payment, the barber calls the next waiting customer (if
any). If such a customer exists, that customer sits in the barber's chair
and the barber starts the next haircut. If no customer is waiting, the
barber goes back to sleep.
-
Each customer follows the following sequence of events.
-
When the customer first enters the barbershop, the customer leaves
immediately if more than 20 people are waiting (10 standing and 10
sitting). On the other hand, if the barbershop is not too full, the
customer enters and waits.
-
If at least one barber is sleeping, the customer looks at a sign, wakes
up the barber who has been sleeping the longest, and sits in that barber's
chair (after the barber has stood up).
-
If all barbers are busy, the customer sits in a waiting-room chair, if one
is available. Otherwise, the customer remains standing until a
waiting-room chair becomes available.
-
Customers keep track of their order, so the person sitting the longest is
always the next customer to get a haircut.
-
Similarly, standing customers remember their order, so the person standing
the longest takes the next available waiting-room seat.
For this Lab you are to write a program to simulate activity for
this barbershop:
-
Altogether, 30 customers should try to enter.
-
Use a randon number generator, so a new customer arrives every 1, 2, 3, or
4 seconds. (This might be accomplished by an appropriate statement
sleep(1+(rand()%4)); .
-
Similarly, use a random number generator, so each haircut lasts between 3
and 6 seconds.
-
Each barber should report when he/she starts each haircut and when he/she
finishes each haircut.
-
Each customer should report when he/she enters the barbershop. The
customer also should report if he/she decides to leave immediately.
-
Similarly, if the customer must stand or sit in the waiting room, the
customer should report when each activity begins.
-
Finally, the customer should report when the haircut begins and when the
customer finally exits the shop.
Work to be turned in:
-
A complete program listing, with appropriate comments for each procedure
and for any logical sections of code.
-
One or two test runs, at least one of which should use the wait times
specified above.
-
A (short) written commentary or run annotation indicating what sequences of
events within the program are effectively tested by the test runs. (E.g.,
are any processes blocked or sleeping at any point, are any activities
happening in an expected sequence?).
Use the format for submitting
assignments for organizing the above pieces.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/213.fa98/lab-barbershop.html
created October 3, 1998
last revised October 3, 1998