Overview
Philosophers is a concurrency simulation project based on the dining philosophers problem. The goal is to model several workers that share limited resources while keeping their timing, state transitions, and synchronization correct.
Visit the repository for source and setup instructions.
Usage
Build from the philo directory with make, then run the simulation with the
required timing arguments:
cd philo
make
./philo 5 800 200 200
The optional fifth argument sets how many meals each philosopher must eat before the simulation stops:
./philo 5 800 200 200 7
What I Built
- Modeled concurrent workers using threads, mutexes, shared state, and timing constraints.
- Coordinated fork access with mutexes and ordered lock acquisition to reduce deadlock risk.
- Implemented monitoring logic for starvation, meal quotas, worker state, and simulation shutdown.
- Practiced debugging timing-sensitive behavior in a small concurrent system.
Details
Shared state and synchronization
Each worker owns its own thread and tracks its last meal time and meal count. Forks are represented by mutexes, while printing and simulation state use their own mutexes so status output and shutdown decisions stay consistent.
Monitoring and shutdown
A monitor thread checks whether a philosopher has exceeded time_to_die or
whether every philosopher reached the optional meal quota. Once a terminal
condition is reached, the simulation state is updated and workers exit their
routines cleanly.
Project rules and limitations
The project is intentionally small and timing-sensitive. It handles the one-philosopher edge case separately and uses short sleeps to avoid busy-waiting too aggressively while still checking shutdown state frequently.
Notes
The project is useful as a compact exercise in thread coordination, shared state ownership, and the kinds of edge cases that show up when timing and synchronization interact.