Scenario 1: Sleeping Barber Problem
The Sleeping Barber Problem 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.)
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 when no customer is waiting in that 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 that 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.
-
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.
Scenario 2: Baboons Crossing a Canyon
This problem is described in Operating Systems: Design and Implementation,
Second Edition by Tannenbaum and Woodhull.
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.
- Simulate until 50 baboons have crossed each way, then stop.
- baboons arrive every 1, 2, 3 or 4 seconds, randomly coming from either the east or the west with equal probability.
-
traversal of the canyon takes 3, 4, 5 or 6 seconds with equal probability.
-
all baboons currently on the rope crossing the canyon must be moving in the same direction
-
Your solution should avoid starvation (i.e. making a baboon wait indefinitely). so, for example, when a baboon that wants to cross
from the east arrives at the rope and finds baboons crossing from the west, he
waits until the rope in empty, but no more baboons arriving from the west are
allowed to start until at least one baboon has crossed the other way.
Scenario 3: Roller Coaster 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) are in the amusement park.
-
There is only one track on the roller coaster, so 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 after it has filled up and started moving.
-
After getting a ride, each passenger wanders 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.
-
Values m, n, S, C, and W must be program constants that can be changed in one place in your simulation code.
-
Run the simulation for 120 time units.
-
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.)