Semaphores: Traffic Light Simulation Program

This program simulates a traffic intersection where multiple roads converge, modeled to mimic real-world traffic light behavior. The simulation utilizes threads to represent vehicles and semaphores to control the traffic lights, ensuring safe and smooth traffic flow without collisions or deadlocks.

The program accepts command-line arguments to specify the number of threads, the limit of the range, and the computational mode. The arguments are provided in the following format:

Each road is equipped with a traffic light, controlled by a semaphore. The light cycles through:
Green: Vehicles proceed.
Yellow: Transitional phase where new vehicles are blocked, but current vehicles finish crossing.
Red: Vehicles are stopped.

Each vehicle is represented by a thread. Threads simulate the behavior of vehicles:
Arriving at the intersection.
Waiting for their traffic light to turn green (semaphore acquired).
Crossing the intersection when allowed.

Vehicles approach the intersection at random intervals, implemented using sleep functions with randomized durations.

Semaphores ensure no conflicting traffic directions (e.g., north-south and east-west) have green lights simultaneously. When one road’s semaphore is released (green light), the others are blocked (red light).

The program uses a timer to simulate realistic traffic light durations:
Green: 30 seconds.
Yellow: 5 seconds.
Red: Corresponding to the other lights' green/yellow duration.

Description of the GIF Link to project

Contact

Las Vegas, Nevada

(951) 454-5593

athertonharrison@gmail.com