This project simulates the classic Bus Problem from concurrent programming, implemented in Java using semaphores.
The simulation models a bus that arrives at a bus stop with multiple riders waiting. Only up to 50 riders can board at once, and the synchronization ensures safe concurrent access and communication between bus and riders.
You can use the provided Makefile for easy compilation and execution.
makeCompiles all .java files and runs the simulation.
make compileCompiles all .java files into .class files without running the program.
make runRuns the most recently compiled simulation.
make cleanDeletes all compiled .class files.
- Buses and riders continue to arrive throughout the day.
- The inter-arrival times of buses and riders are exponentially distributed with means of 20 minutes and 30 seconds, respectively.
- Each bus allows up to 50 riders to board.
- Riders who arrive while boarding is in progress must wait for the next bus.
- Each rider signals after boarding, and the bus departs only after all selected riders are on board.
This problem demonstrates:
- Semaphores for synchronization
- Critical section protection using
mutex - Rendezvous pattern between bus and riders
.
├── Makefile
├── BusProblem.java
├── Rider.java
├── BusSimulation.java # Main simulation class
└── README.md
Rider 1 arrived.
Rider 2 arrived.
Bus arrived with 5 riders waiting.
Rider 1 boarded.
Rider 2 boarded.
Bus departing with 2 riders.
Rider 3 arrived.
...
These print statements visualize how riders and buses interact in real time, demonstrating synchronization correctness.
- Mutual exclusion using semaphores (
mutex) - Bounded waiting (max 50 riders per bus)
- Rendezvous synchronization (
bus,boarded) - Randomized exponential event simulation
If you wish to recompile or reset the simulation environment:
make clean
makePasindu Dulmith & Adeesha Dissanayake
University of Moratuwa – Department of Computer Science & Engineering
4th Year Undergraduates | Data Science & Main Stream Specializations