A two-way north-south highway through the mountains must pass through a narrow t
ID: 3745663 • Letter: A
Question
A two-way north-south highway through the mountains must pass
through a narrow tunnel with only one lane. A south-bound or
north-bound car can pass through the tunnel only if, when it arrives,
there are no on-comming cars in the tunnel. Note, due to the
length of the tunnel, at most only 3 cars are allowed in the tunnel
at one time—provided that they are all heading in the same
direction.
In pseudo-code write the two processes north2south and south2north
that allows cars to use the tunnel safely. As a starting point use
the code below.
Remember: All Semaphores and variables must be initialised.
/* Shared Data between all processes */
Semaphore tunnel_mutex;
/* Shared Data between south2north processes */
Semaphore north_mutex;
Semaphore going_north_max;
int number_going_north;
south2north()
{
/* Add Synchronisation code here */
DriveNorthThroughTunnel();
/* Add Synchronisation code here */
}
/* Shared Data between north2south processes */
Semaphore south_mutex;
Semaphore going_south_max;
int number_going_south;
north2south()
{
/* Add Synchronisation code here */
DriveSouthThroughTunnel();
/* Add Synchronisation code here */
}
Explanation / Answer
SOLUTION:
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;
}
};
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.