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:
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 exercise, you are to write a C program to simulate
activity for this barbershop:
-
Simulate each barber and each customer as a separate process.
-
Altogether, 30 customers should try to enter.
-
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)); .
-
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.
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.
-
The roller coaster contains m cars (default 2).
-
On a particular day, n passengers (default value 10).
-
Since there is only one track, cars cannot pass each other, i.e., they must
finish going around the track in the order in which they started.
-
For passenger safety, one car can start no sooner than S seconds (default 4)
after the previous car leaves.
-
Passengers arrive and enter a car. However, a car holds C passengers
(default 4), and the car does not start moving until it is full.
-
The car takes the same T seconds (default 12) to go around the track each
time it fills up.
-
After getting a ride, each passenger wanders (sleep procedure) around the
amusement park for a random amount of time between 0 and W seconds (default
5) before returning to the roller coaster for another ride.
For this exercise, you are to write a C program to simulate
activity for this roller-coaster problem:
-
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.
-
Each passenger and each car should be simulated as a separate process.
-
Run the simulation for R seconds (default 120). (Again, this may be a
program constant or a command-line argument - your choice.)
-
Compute the average time that a passenger must wait to be seated after
deciding to take a ride.
-
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:
-
Several baboons can cross at the same time, provided that they are all
going in the same direction.
-
If eastward moving and westward moving baboons ever get onto the rope at the
same time, a deadlock will result (the baboons will get stuck in the
middle) because it is impossible for one baboon to climb over another one
while suspended over the canyon.
-
If a baboon wants to cross the canyon, he must check to see that no other
baboon is currently crossing in the opposite direction.
For this exercise, you are to write a C program to simulate
activity for this canyon crossing problem:
-
Simulate each baboon as a separate process.
-
Altogether, 30 baboons will cross the canyon, with a random number
generator specifying whether they are eastward moving or westward moving
(with equal probability)..
-
Use a random number generator, so a new baboon arrives every 1, 2, 3, or 4
seconds.
-
Similarly, use a random number generator, so each traversal of the canyon
takes between 3 and 6 seconds.
-
Use semaphores for synchronization. (Additional communication via sockets
is allowed, but do not use sockets unless such communication is clearly
needed.)
-
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.
-
A complete program listing, with appropriate comments for each procedure
and for any logical sections of code.
-
One or two test runs.
-
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.cs.grinnell.edu/~walker/courses/213.fa04/lab-barbershop.shtml