Question
#3 The one-lane bridge problem. Utilize the beginnings of the bridge.c file to solve this problem. Cars coming from the north and the south arrive
#3 The one-lane bridge problem. Utilize the beginnings of the bridge.c file to solve this problem. Cars coming from the north and the south arrive at a one-lane bridge. Cars heading in the same direction can cross the bridge at the same time, but cars heading in opposite directions cannot. And, if there are ever more than 3 cars on the bridge at one time, the bridge will collapse under their weight. In this system, each car is represented by one thread, which executes the procedure OneVehicle when it arrives at the bridge: OneVehicle() { ArriveBridge(dir); CrossBridge(dir); ExitBridge(dir); } In the code above, dir is either 0 (from the north) or 1 (from the south); it gives the direction in which the vehicle will cross the bridge. Write the procedures ArriveBridge and ExitBridge (the CrossBridge procedure should just print out a debug message) using monitor (mutex and condition variables). ArrivingBridge must not return until it is safe for the car to cross the bridge in the given direction (it must guarantee that there will be no head-on collision). ExitBridge is called to indicate that the caller has finished crossing the bridge; ExitBridge should take steps to let additional cars cross the bridge. In the main thread, you need to create 50 child threads, one for each car. The driving direction for each car should be randomly assigned. Make sure that your simulation outputs information that clearly shows that your solution works. The message should indicate car number and driving direction. In particular, messages should be printed at the following times: - whenever a car arrives a bridge. - whenever a car is crossing the bridge. - whenever a car exits from the bridge. Also, insert a sched_yield()as the last line in each of the functions; this will increase the chance of interleaving and hence test your program more thoroughly.
// bridge.c
void OneVehicle() { int direc = rand() % 2; ArriveBridge(direc); CrossBridge(direc); ExitBridge(direc); } void CrossBridge (int direc) { //crossing the bridge; } monitor Bridge { Condition safe; int currentDirec; int currentNumber; void ArriveBridge(int direc) { while (!isSafe(direc) ) safe.Wait(); currentNumber++; currentDirec = direc; } void ExitBridge(Bridge::Direction direc) { currentNumber--; safe.Broadcast(); } bool isSafe(int direc) { if ( currentNumber == 0 ) return TRUE; // always safe when bridge is empty else if ((currentNumber < 3) && (currentDirec == direc)) return TRUE; // room for us to follow others in direc else return FALSE; // bridge is full or has oncoming traffic. } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started