monitor BusyBridge {
int eastNum = 0, westNum = 0; // Number of
cars in each direction
condition busy ;
// monitor condition variable
public:
ArriveBridge (Direction direc)
{
// wait for any cars who are already going in opposite direction or if there are already 5 cars on the bridge
if ( East ==
direc && ( westNum || ( 5 == eastNum ) ) ) // east is our
direction but cars are going on in west direction
// or there are already 5 cars present on bridge in east
direction
busy.wait;
if ( West ==
direc && ( EastNum || ( 5 == westNum ) ) )// west is our
direction but cars are going on in east direction
// or there are already 5 cars present on bridge in west
direction
busy.wait;
// now
increment counter of cars heading in our direction
if (East ==
direc)
eastNum++;
if (West ==
direc)
westNum++;
}
ExitBridge (Direction direc) {
// if this
the last car off heading east then we can notify all westwards
traffic that it can proceed further
if (East ==
direc && 0 == --eastNum)
while (busy.queue) busy.signal;
// if this is
the last car off heading west then we can notify all eastwards
traffic that it can proceed further
else if (West ==
direc && 0 == --westNum)
while (busy.queue) busy.signal;
}
}
3- (Bonus +15) There is a one-way narrow bridge on a public highway. Traffic may only cross the bridge in one direction at a time. The bridge can tolerate the weight of at most 5 vehicles. Each car w...
Write a multithread program in Java, C++ or C# language which
simulate the below Bridge problem. The program should accept the
arrival ratio in each direction and show the bridge status and
queues in each side.
3- (Bonus +15) There is a one-way narrow bridge on a public highway. Traffic may only cross the bridge in one direction at a time. The bridge can tolerate the weight of at most 5 vehicles. Each car which arrives the bridge calls the...