[2 hwp] One-lane bridge with semaphores. There runs a river between two villages A and B and the villages are connected by a narrow
one-lane bridge, where cars are allowed to drive only in one direction in a time. When cars are
passing the brigde from A to B, then the cars willing to cross the bridge from B to A have to
wait. The bridge is noy very sturdy, so maximum amount of 10 cars are allowed on the bridge
at a time. The car processes call procedure enter_bridge(direction) before using the bridge, and
procedure exit_bridge(direction), when thy leave the bridge.
Process car [i = 1 to N] {
.....
enter_bridge(direction);
drive over the bridge
exit_bridge(direction);
......
}
Give the code for procedures enter_bridge(direction) and exit_bridge(direction). The solution
must be based on semaphores and P and V operations. The solution needs not to be fair, which
means that the waiting times on the other end may be very long. When the waiting ends, the cars
must be allowed to proceed in FCFS order. If you want, you can try out your algorithms in BACI, by placing the into the bridge simulator dummy solution. (While running the simulation, observe the values of shared variables west_bound and east_bound, and check that the "animated bridge" has proper number of of cars. You may tune the system parameters better if you so wish. If you have too much time, you may want to tune the simulation graphics better - see jBACI Concurrency Simulator User Guide).
Check that your algorithm works at least in the following cases:
- First car comes to direction A or B?
- There are 5 cars on the bridge toward A, and a new one arrives towards A or B?
- There are 4 cars on the bridge toward A, a new one arrives towards B, and then a new car towards A?
- There are cars on bridge, and they leave making bridge empty. There were cars waiting in th eother direction.
- There are cars on bridge, and they leave making bridge empty. A new cars arrives towards A or B?
- Modify your answer to the situation where there can be at most 3 cars on the bridge due to weight limitiations.