Ex. 1 Download the following files in a special folder:
bridge.h
bridge.cc
main.cc
Makefile
Compile with the command make and run it with the command
bridge.
This program simulate a one-lane bridge. If the bridge is free, it can be crossed by cars in any direction. Cars heading in the same direction can cross the bridge in the same time, but cars heading in opposite directions cannot.
The program implements a monitor simulating an access control for the bridge. A car entering the bridge must call the function request in that direction, and wait until the monitors grants permission to go. When the car leaves the bridge it must call the function release in the same direction, allowing other cars to cross the bridge.
The monitor uses two conditional variables, one for each direction, a variable counting the cars engaged in crossing the bridge, and two counters for the cars waiting in each direction.
If the bridge is not free, a car requesting access to the bridge increases the number of cars waiting in the requested direction, then waits on the conditional variable for that direction. Once it has been signaled, it increments the number of cars crossing the bridge.
A car leaving the bridge decreases the number of cars crossing. This way the last one to get out broadcasts the conditional variable in the opposite direction if any car is waiting on it, setting the waiting number for that direction to 0 in the same time. If no car is waiting in the opposite direction, the procedure is applied to the cars going in the same direction (if any).
In the current implementation, only the functionality allowing the
cars to cross to the left is implemented. You'll have to implement the
same thing for the cars going to the right, meaning that you have to
complete the following functions:
request_right() in bridge.cc
release_right() in bridge.cc
Car() in main.cc, needs the else completed.
Turn in (optional, 1 extra points): the modified source files. Only accepted till the final.
Ex. 2 (2 pt, optional) Specify what the monitor invariant is and justify (by email).