Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
       }
}

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT