Laboratory Exercises For Computer Science 213

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:

Diagram of Barbershop
Three barbers work independently in a barber shop:

For this Lab you are to write a program to simulate activity for this barbershop:
  1. Altogether, 30 customers should try to enter.
  2. 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)); .
  3. Similarly, use a random number generator, so each haircut lasts between 3 and 6 seconds.
  4. Each barber should report when he/she starts each haircut and when he/she finishes each haircut.
  5. Each customer should report when he/she enters the barbershop. The customer also should report if he/she decides to leave immediately.
  6. Similarly, if the customer must stand or sit in the waiting room, the customer should report when each activity begins.
  7. Finally, the customer should report when the haircut begins and when the customer finally exits the shop.


Work to be turned in:

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