Here are a couple of synchronization problems for
practice. Solve each using a semaphore
solution. Solve using a monitor
solution. Solutions will be discussed
Monday, July 14th, after class.
- Larry, Moe, and Curly are
planting seeds. Larry digs the holes. Moe then places a seed in each hole.
Curly then fills the hole up.
There are several synchronization constraints:
- Moe cannot plant a
seed unless at least one empty hole exists, but Moe does not care how far
Larry gets ahead of Moe.
- Curly cannot fill a
hole unless at least one hole exists in which Moe has planted a seed, but
the hole has not yet been filled. Curly does not care how far Moe gets
ahead of Curly.
- Curly does care that
Larry does not get more than MAX holes ahead of Curly. Thus, if there are
MAX unfilled holes, Larry has to wait.
- There is only one
shovel with which both Larry and Curly need to dig and fill the holes,
respectively.
Sketch out the pseudocode for the 3 processes which
represent Larry, Curly, and Moe using semaphores as the synchronization
mechanism.
- 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. He locates a deep canyon and fastens a rope
across it, so the baboons can cross hand-over-hand. 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, it must check to see that no other baboon is currently
crossing in the opposite direction. Write a program that avoids deadlock.
Do not worry about a series of eastward moving baboons holding up
the westward moving baboons indefinitely (starvation).