philosophers
Philosophers is a project developed as part of the 42 School curriculum, aimed at simulating the dining philosophers problem in C. It focuses on concurrency, thread management, and synchronization using mutexes, helping students deepen their understanding of multithreading, deadlocks, and low-level systems programming.
Last updated
Philosophers
Usage
Prerequisites
Clone the repository to your local machine using the following command in the terminal.
git clone https://github.com/milandekruijf/philosophers.git && cd philosophersCompiling
In order to compile the program, run the following command in the project root directory.
makeCompiles using the cc compiler with the flags -Wall, -Wextra, and -Werror, and includes debug symbols (-g3) by default.
Advanced compiler options
To build without -Wall, -Wextra, -Werror, and -g3, run:
make STRICT=0To link with AddressSanitizer (useful when investigating memory issues), run:
make TEST=1Running
After compiling, the executable philo is created in the /out directory. It expects four or five numeric arguments: the number of philosophers, time to die (ms), time to eat (ms), time to sleep (ms), and optionally the number of times each philosopher must eat before the simulation stops.
./out/philo 5 800 200 200./out/philo 5 800 200 200 7Each line printed to standard output is a timestamp in ms since the start of the simulation, the philosopher number, and an action (is thinking, has taken a fork, is eating, is sleeping, or died).
Cleaning
Remove object files, or remove the entire build output directory, with:
make cleanmake fcleanRebuild from scratch:
make reFeatures
This project implements the mandatory philosophers version using pthreads and mutexes (one mutex per fork, plus a mutex for synchronized printing).
Simulation
- One thread per philosopher runs a repeating routine of thinking, eating, and sleeping.
- A separate monitor thread watches for death (when a philosopher exceeds time to die since their last meal) and, when the optional meal count is set, detects when every philosopher has eaten enough times.
- Timestamps use
gettimeofday-style timing so logs reflect elapsed milliseconds from the start of the run.
Synchronization
- Forks are modeled as mutexes; philosophers lock the mutex for their left and right fork before eating.
- Even-numbered and odd-numbered philosophers pick up forks in opposite orders to reduce the risk of deadlock when many philosophers compete for the same forks.
Arguments
- Five arguments: simulation runs until a philosopher dies or the process is interrupted.
- Six arguments: the last value is the minimum number of meals per philosopher; when all philosophers have reached that count, the simulation ends cleanly.
Limitations
- This repository implements the thread-and-mutex variant only (not a separate processes-and-semaphores bonus binary).
- Arguments must be non-negative integers in the expected positions; invalid input results in an error message and a non-zero exit status.