Multithreaded Operating Systems Project — Java concurrency simulation demonstrating deadlock avoidance, starvation prevention, and centralized monitor synchronization.
This project simulates a concurrent ferry transportation system where 30 independent vehicle threads (12 Cars, 10 Minibuses, 8 Trucks) and a single Ferry thread operate between Side A and Side B. The simulation demonstrates robust solutions to classic Operating System challenges including:
- 🔄 Race Conditions
- ⛔ Deadlock Avoidance
- ⏳ Starvation Prevention
- 📊 Fair Scheduling (FIFO)
- 🔒 Mutual Exclusion
The system follows a strict lifecycle: vehicles are generated, pass through toll booths, join a waiting queue, board the ferry without exceeding maximum capacity (20 units), travel to the opposite side, conduct business, and finally return to their original side to terminate.
| Feature | Description |
|---|---|
| Centralized Monitor Pattern | All synchronization operations managed by a single SimulationManager to eliminate nested locks and circular waits |
| Mesa Monitor Pattern | Uses Condition.await() / signalAll() for efficient thread coordination without busy-waiting |
| FIFO Discipline | Strict First-In-First-Out ordering via LinkedList queues; prevents overtaking |
| Bounded Wait (Timeout) | Ferry departs after timeout to prevent indefinite waiting |
| Consecutive Trip Limiter | Forces side switch after consecutive trips to prevent starvation of opposite side |
| No-Sleep-Under-Lock | All simulated delays executed outside critical sections to maximize liveness |
| Dynamic Configuration | Runtime-adjustable parameters via config.txt without recompilation |
| Fair Locking | ReentrantLock(true) with fairness policy to reduce starvation risk |
| State Machine | Each vehicle tracks its exact lifecycle via VehicleState enum |
| Comprehensive Logging | Real-time console logging with timestamps and statistics |
┌─────────────────┐ ┌──────────────────────────────┐ ┌─────────────────┐
│ Car Threads │────▶│ │◀────│ │
├─────────────────┤ │ SimulationManager │ │ Ferry Thread │
│ Minibus Threads │────▶│ (Centralized Monitor) │────▶│ │
├─────────────────┤ │ │ │ │
│ Truck Threads │────▶│ • ReentrantLock (fair) │ │ │
└─────────────────┘ │ • Condition Variables │ └─────────────────┘
│ • FIFO Queues (Side A/B) │
│ • Semaphore (Toll Booths) │
└──────────────────────────────┘
START
│
▼
Acquire Lock
│
▼
Is Head of Queue? ──NO──▶ await()
│YES
▼
Fit in Capacity? ───NO──▶ await()
│YES
▼
Board Ferry & signalAll()
│
▼
Release Lock
│
▼
END
| Component | Responsibility |
|---|---|
SimulationManager |
Centralized monitor: manages all shared resources, queues, locks, and conditions |
Ferry |
Worker thread: manages boarding, travel, and unloading phases |
Vehicle |
Client thread: state machine lifecycle, invokes blocking requests to manager |
Config |
Dynamic configuration loader from config.txt |
TimeManager |
Simulation time tracking |
Logger |
Thread-safe console output |
- Language: Java 17+
- Concurrency:
java.util.concurrent(ReentrantLock, Condition, Semaphore) - Collections:
LinkedList(FIFO queues),ArrayList - Build: Platform-independent (no external dependencies)
- OS Concepts: Monitor pattern, deadlock avoidance, starvation prevention, bounded waiting
- Java JDK 17 or higher
- Terminal / Command Line
# Clone the repository
git clone https://github.com/yourusername/ferry-transport-simulation.git
cd ferry-transport-simulation
# Compile all source files
javac -d . Main.java model/*.java manager/*.java enums/*.java config/*.java
# Run the simulation
java Maincompile.bat
run.batAll simulation parameters can be adjusted in config.txt without recompiling:
# Vehicle Counts
TOTAL_CARS=12
TOTAL_MINIBUSES=10
TOTAL_TRUCKS=8
# System & Vehicle Settings
TOLL_BOOTHS_PER_SIDE=2
CAR_CAPACITY=1
MINIBUS_CAPACITY=2
TRUCK_CAPACITY=3
# Ferry Settings
FERRY_CAPACITY=20
BOARDING_TIMEOUT_SEC=3
# Delays (milliseconds)
TOLL_DELAY_MIN=200
TOLL_DELAY_MAX=600
BOARDING_DELAY_MIN=100
BOARDING_DELAY_MAX=300
TRAVEL_DELAY_MIN=1500
TRAVEL_DELAY_MAX=2500
RETURN_DELAY_MIN=2000
RETURN_DELAY_MAX=4000
TURNAROUND_DELAY=500Initializing Ferry Transport Simulation...
=====================================
[SYSTEM] Configuration loaded from config.txt
-> Cars: 12, Minibuses: 10, Trucks: 8
-> Ferry Capacity: 20
-> Boarding Timeout: 3 sec
-> Toll Delay Range: 200ms - 600ms
=====================================
[Time 2] Ferry is BOARDING on SIDE_A
[Time 5] CAR-1 created on SIDE_B
...
=== SIMULATION STATISTICS ===
Total Simulation Time: 19528 ms
Total Ferry Trips : 8
Average Waiting Time : 2729 ms
Maximum Waiting Time : 7684 ms
Ferry Utilization : 70.00%
=============================
Simulation completed safely. No deadlocks detected!
| Metric | Value |
|---|---|
| Total Vehicles | 30 |
| Ferry Capacity | 20 units |
| Threads | 31 (30 vehicles + 1 ferry) |
| Deadlocks | 0 |
| Average Utilization | ~70% |
| Scheduling | Strict FIFO with starvation prevention |
| Name | Student ID |
|---|---|
| Hasan ÇELİK | 220315024 |
| Emine ÇETİN | 220315040 |
| Mustafa Furkan YILMAZ | 220315082 |
Institution: Manisa Celal Bayar University — Computer Engineering
Course: Operating Systems
This project is licensed under the MIT License - see the LICENSE file for details.
- Manisa Celal Bayar University, Computer Engineering Department
- Operating Systems course instructors