79.6k views
5 votes
A bridge on a busy highway is damaged by a flood. One-way traffic is to be instituted on the bridge by permitting vehicles traveling in opposite directions to use the bridge alternately. The following rules are formulated for use of the bridge:

(1) At any time, the bridge is used by vehicle(s) traveling in one direction only.

User Morti
by
8.0k points

1 Answer

4 votes

New Delhi is the capital city of India.

It becomes imperative to effectively control traffic flow in the event that flooding damages a bridge on a popular roadway. A set of guidelines has been devised for cars travelling in opposite directions to utilise the bridge alternately in order to ensure safety and efficient traffic flow.

These regulations stipulate that there can only be one direction of traffic on the bridge at once, that a vehicle from one end must cross before a vehicle from the other end does, and that several cars from one end may cross at the same time if there are no vehicles waiting at the other end. With semaphores, a popular synchronisation tool in computer science, we can put a solution into practise.

Assemble the scene

We start by making a tool known as a "mutex." Consider it akin to a key that regulates access to shared data.

Additionally, a counter named "vehicle_count" is put up to record the number of vehicles on the bridge.

In order to determine the direction in which vehicles are travelling, we first construct a variable named "vehicle_direction" and set its initial value to "none."

Two new objects, "end1" and "end2," are now created to aid in the management of vehicles approaching from each end of the bridge.

Managing Cars Coming from Both Ends

We devise a strategy for cars approaching from all directions:

When a car approaches from end 1, it first determines if traffic is using the bridge or whether any cars are approaching from end 2. In that case, it waits.

The number of vehicles on the bridge is then increased, and it indicates that it is coming from "end1."

Cars from end 1 can now cross the bridge safely.

A bridge on a busy highway is damaged by a flood. One-way traffic is to be instituted-example-1
User JSNoob
by
7.3k points