CSC 213:  Operation Systems and Parallel Algorithms

Some Multi-processing Problems Which Might Involve Deadlock or Starvation

Goals: This laboratory exercise provides practice process coordination using semaphores.

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

Exercise 1: Barbershop Problem Simulation

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 a new customer arrives every 1, 2, 3, or 4 seconds. (This might be accomplished by an appropriate statement sleep(1+(rand()%4)); .
  4. Similarly, use a random number generator, so 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.

Exercise 2: Roller Coaster with One Car and with Several Cars

This exercise is a slightly modified version of exercises 4.9 and 4.10 in Stephen Hartley, Concurrently Programming: The Java Programming Language.

An amusement part includes a roller coaster ride. Use of the roller coaster follows these rules.

For this exercise, you are to write a C program to simulate activity for this roller-coaster problem:

  1. Values m, n, S, C, and W may be set as program constants, but you may specify them as command-line arguments if you prefer.
  2. Each passenger and each car should be simulated as a separate process.
  3. Run the simulation for R seconds (default 120). (Again, this may be a program constant or a command-line argument - your choice.)
  4. Compute the average time that a passenger must wait to be seated after deciding to take a ride.
  5. Use semaphores for synchronization. (Additional communication via sockets is allowed, but do not use sockets unless such communication is clearly needed.)

Exercise 3: 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 a new baboon arrives every 1, 2, 3, or 4 seconds.
  4. Similarly, use a random number generator, so each traversal of the canyon takes between 3 and 6 seconds.
  5. Use semaphores for synchronization. (Additional communication via sockets is allowed, but do not use sockets unless such communication is clearly needed.)
  6. Your solution should avoid starvation. When a baboon that wants to cross to the east arrives at the rope and finds baboons crossing to the west, he waits until the rope in empty, but no more westward moving baboons are allowed to start until at least one baboon has crossed the other way.


Work to be turned in:

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

Use the format for submitting assignments for organizing the above pieces.


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-barbershop.shtml

created July 20, 1998
last revised December 10, 2004
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu)