Programming Assignment III

Due on Friday, October 30, 2009 @ 11pm

70 points

Baboons Crossing a Canyon

A canyon cuts through the territory of a colony of baboons. The baboons use a rope stretching across the canyon to cross from one side to the other. The rope is strong enough to permit any number of baboons to cross in the same direction at the same time. However, the rope is too thin for the baboons to cross the canyon in both directions at the same time. Consequently, a baboon that wants to cross from west to east must wait until all westward-moving baboons have finished crossing and the rope is free. If the rope is being used by westward-moving baboons, then other baboons may start to cross from east to west no matter how many eastward-moving baboons are waiting on the other side.

Unfortunately, this simple protocol could cause starvation. Why? Figure it out yourself. To overcome this problem, when a baboon that wants to cross to the east (resp, west) arrives at the rope and finds baboons crossing to the west (resp., east), it waits until the rope is empty, but no more westward-moving (resp., eastward-moving) baboons are allowed to start crossing until at least one waiting baboon has crossed the other way.

In the system, baboons are simulated by threads. Initially, there are e > 0 baboons going eastward, and w > 0 baboons going westward. Each baboon makes t > 0 trips and then disappears forever (i.e., exits the system). Each eastward-moving (resp., westward-moving) baboon goes to the east (resp., west) end and magically comes back to the west (resp., east) end for the next trip. This repeats t times. The following shows a template of a baboon thread, where DIRECTION is the moving direction, and GetOnRope() and GetOffRope are monitor procedures:

for (int i = 1; i <= t; i++) {
     Delay();                     // plays for a while       
     GetOnRope(DIRECTION, ...);   // makes a request to move on the rope
     Delay();                     // has the permission     
                                  // gets on the rope and        
                                  // moves for a while
     GetOffRope(DIRECTION, ...);  // gets off the rope        
     // at the other end now, print message         
     // magically comes back
}

Your monitor maintains all necessary information to make sure baboons will cross the canyon based on the give conditions. In particular, your monitor must guarantee the following:

  1. Baboons on the rope only move in the same direction.
  2. Baboons must use the rope to cross the canyon. In other word, baboons cannot magically cross the canyon and magically come back. This magic only applies to the returning trip.
  3. No baboon can get on the rope without a permission.
  4. When a baboon arrives and there are baboons on the rope moving in the opposite direction, no baboon can get on the rope and move in the opposite direction. More precisely, if a eastward-moving baboon arrives and there are a number of westward-moving baboons on the rope, then no other westward-moving baboons, whether they are waiting or just arrive, can be on the rope.

Write a C++ program using ThreadMentor to simulate this activities. You can only use one monitor to solve this problem. Your program will not be counted as a correct one if any other synchronization primitives are used.

Input and Output

The input of your program consists of the following:

Submission Guidelines

  1. All programs must be written in C++ using ThreadMentor.
  2. Use the submit command to submit your work. You can submit as many times as you want, but only the last one will be graded. A makefile is required. Name your executable prog3.
  3. I will use the following approach to compile and test your programs:
    make                <-- make your program
    prog3 e w t         <-- test your program
    
    I may repeat the above procedure several times to see if your program performs correctly.
  4. Your implementation should fulfill the program specification as stated. Any deviation from the specification will cause you to receive zero point.
  5. You should aim for maximum parallelism. That is, implement the solution as stated and make sure all required threads will execute simultaneously. Without doing so, you risk very low grade.
  6. No late submission will be graded.
  7. My rule of grading is deduction based. I assume you always receive 100%. If you missed something, some points will be taken off based on the following rules. Read these rules carefully. Otherwise, you could receive low or very low grade. Click here to see how your program will be graded.
  8. Since the rules are all clearly stated above, no leniency will be given. So, if you have anything in doubt, please ask for clarifications.
  9. Click here to see how your program will be graded.

A Hint

Study the readers-writers and bridge-crossing problems and their solutions carefully.