Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Rotary Park, there is one lane bridge. Vehicles coming from the south and the

ID: 3736698 • Letter: I

Question

In Rotary Park, there is one lane bridge. Vehicles coming from the south and the north arrive at this one-lane bridge. Vehicles heading in the same direction can cross the bridge at the same time, but vehicles heading in opposite directions cannot. And, if there are ever more than two vehicles on the bridge at one time, the bridge will collapse under their weight. If a vehicle arrives at the bridge, the number of vehicles on the bridge is less than two and they all head in the same directions, this new vehicle should be allowed to cross the bridge without waiting. Use semaphore to coordinate vehicles’ crossing the one-lane bridge. Write it in a pseudo code format

Explanation / Answer

def control_bridge
  
lock_bridge = false
number_of_vehicle_on_bridge = 0
moving_towards as null
  
when new vehicle arrives
if lock_bridge is false
if number_of_vehicle_on_bridge is 0
number_of_vehicle_on_bridge + 1
if moving to North
moving_towards is north
else
moving_towards is south
  
else if number_of_vehicle_on_bridge is 1
if moving_towards is equal incoming vehicle
number_of_vehicle_on_bridge +1
lock_bridge is true
else
wait until vehicle crosses the bridge in opposite direction

else if number_of_vehicle_on_bridge is 2
wait until bridge available

else
wait until bridge available

Here lock_bridge acts as a semaphore which controls the bridge based on both count of the vehicle and direction.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote