Microsoft Word-Exam Monitors : a search proto.csqcaryeducs7 15 - + Automatic Zoo
ID: 3729938 • Letter: M
Question
Microsoft Word-Exam Monitors : a search proto.csqcaryeducs7 15 - + Automatic Zoom # Q3. (35 points) Monitors 3.1 Synchronization of automobile traffie through a one-way tunnel Suppose a two-way (two-lane) north-south road contains a one-lane tunnel. A southbound (or northbound) car can use the tunnel only if when arrives at the entrance the tunnel, there are no oncoming cars . in the tunnel. When a car approaches the tunnel, it notifies the controller computer by calling a method orthboundArrival or southboundArrival depending on the direction of travel of the car. When a car exits the tunnel, it notifies the tunnel controller computer by calling a function named depart. The direction of the traffic should be mentioned. a) Develop a monitor to synchronize the automobile traffic (draw the monitor picture. give the thread execution sequence, give service mcthods pscudo-code, initialization, condition variables). use the concept of monitors: cond. var. have names, are implemented as queues with FIFO policy. You can use any of the two signal policies but specify which one you use. Don't use notifyaii. Briefly comment your implementation policy? If no, explain why notExplanation / Answer
monitor traffic_tunnel
{
int nbound = 0, sbound = 0; // initializing as no vehicles in north and south side of tunnel
traffic-signal nbound_signal = RED,sbound = RED;
condition busy;
public:
northboundArrival() // called by a vehicle coming from north side
{
if(sbound > 0) busy.wait; // it checks whether there is any vehicle is in south side and if there is any vehicle, then it wait
nbound++; // increments the number of vehicles in north direction if no vehicle in south side
nbound_signal = GREEN; // set the traffic light as green in north side
sbound_signal = RED; // set the traffic light as red in south side which blocks vechiles from south to enter the tunnel
};
southboundArrival() // called by vehicle entering tunnel from south direction
{
if(nbound > 0) busy.wait; // it checks whether there is any vehicle is in north side and if there is any vehicle, then it wait
sbound++; // if there is no vehicle in north side,then it increments the number of vehicle from south direction
sbound_signal = GREEN; // signals the traffic light as green in south direction
northbound_signal = RED; // signals the traffic light to red in north direction which will block any vehicle entering tunnel from north side
};
depart( Direction exit) // called when a vehicle exists the tunnel
{
if(exit == north) // checking whether the exited vehicle was from north
{
nbound--; // decrementing the count of vehicles in north side
if(nbound == 0)
while(busy.queue) busy.signal;
}
else if(exit == south) // checking whether the exited vehicle was from south
{
sbound--; //decrementing the count of vehicles in south side
if(sbound == 0)
while(busy.queue) busy.signal;
}
};
}