CSC 213, Fall 2006 : Schedule : Lab 12

Lab 12: Classic Problems in Concurrent Programming

Goals: 

Collaboration: You will complete this lab in teams of 2-3 of your choice.  You may consult me or your classmates with proper attribution.

Overview: In this lab, you are to write a C program to solve ONE of the following two problems.


Option 1: The Barbershop 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 exercise considers the following version:

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

For this exercise, you are to write a C program to simulate activity for this barbershop:

  1. Simulate each barber and each customer as a separate process.
  2. Altogether, 30 customers should try to enter.
  3. Use a random number generator so that a new customer arrives every 1, 2, 3, or 4 seconds. (This might be accomplished by a statement such as sleep(1+(rand()%4)); .
  4. Similarly, each haircut lasts between 3 and 6 seconds.
  5. Each barber should report when he/she starts each haircut and when he/she finishes each haircut.
  6. Each customer should report when he/she enters the barbershop. The customer also should report if he/she decides to leave immediately.
  7. Similarly, if the customer must stand or sit in the waiting room, the customer should report when each activity begins.
  8. Finally, the customer should report when the haircut begins and when the customer finally exits the shop.
  9. Semaphores and shared memory should be used for synchronization.

Option 2: Baboons Crossing a Canyon

This exercise is a slightly modified version of exercises 2.35 and 2.36 in Tannenbaum and Woodhull, Operating Systems: Design and Implementation, Second Edition.

A student majoring in anthropology and minoring in computer science has embarked on a research project to see if African baboons can be taught about deadlocks. She locates a deep canyon and fastens a rope across it, so the baboons can cross hand-over-hand.

Passage along the rope follows these rules:

For this exercise, you are to write a C program to simulate activity for this canyon crossing problem:

  1. Simulate each baboon as a separate process.
  2. Altogether, 30 baboons will cross the canyon, with a random number generator specifying whether they are eastward moving or westward moving (with equal probability).
  3. Use a random number generator, so the time between baboon arrivals is between 1 and 8 seconds.
  4. Each baboon takes 1 second to get on the rope.  (That is, the minimum inter-baboon spacing is 1 second.)
  5. All baboons travel at the same speed.  Each traversal takes exactly 4 seconds, after the baboon is on the rope.
  6. Use semaphores for synchronization. You may also use shared memory.  (Additional communication via sockets is allowed, but do not use sockets unless such communication is clearly needed.)

What to Turn In

Write a program to solve ONE of the exercises given above.

Part A: Due Monday, 4 December, 2006:
Part B: Due Friday, 8 December, 2006 at 5 p.m.:

Janet Davis (davisjan@cs.grinnell.edu)

Created November 26, 2006
Last revised December 5, 2006
With thanks to Henry Walker