Concurrency & Algorithms
Philosophers: Threads, Mutexes & Semaphores in C
A C solution to the dining philosophers problem, with a thread-and-mutex version and a process-and-semaphore version that avoid deadlock, starvation, and data races.
Last updated:

Introduction
Philosophers is a 42 concurrency project based on the classic dining philosophers problem. Philosophers sit around a table with one fork between each pair; to eat, each one needs both neighboring forks, and anyone who goes too long without eating dies. The program takes the number of philosophers and the times to die, eat, and sleep, and prints every state change with a millisecond timestamp.
There are two implementations: the mandatory version runs each philosopher as a thread with one mutex per fork, and the bonus version runs each philosopher as a separate process that takes forks from a shared semaphore.
The problem
Shared resources and strict timing are a hard combination. If every philosopher grabs one fork at the same moment, the program deadlocks; if some always win the race for forks, others starve; and if a shared value is read while another thread writes it, the behavior is undefined. On top of that, a death must be reported within 10 ms, and nothing may be printed after it.
The goal was a simulation that never deadlocks, keeps everyone alive whenever the timings allow it, reports deaths precisely, and has no data races.
Architecture
In the mandatory version, shared data holds the timing parameters, one mutex per fork, a print lock, and a lock that protects the death flag and meal counter. Each philosopher thread loops through thinking, taking its two forks, eating, and sleeping, while the main thread acts as a monitor that checks each philosopher's last meal time and stops the simulation at the first death or once everyone has eaten enough.
In the bonus version, each philosopher is a forked process. The forks are a counting semaphore, printing and shared state are protected by binary semaphores, and each process runs a watcher thread that checks its own deadline. The parent waits for the first process to exit and then kills the others, while a separate thread counts completed meals.
Implementation decisions
- Staggered start
- Even-numbered philosophers start first and odd-numbered ones a moment later, so neighbors do not all reach for the same fork at once. That breaks the symmetry that causes deadlock and spreads access to the forks evenly.
- A single monitor
- Rather than each thread checking whether it should die, one monitor scans every philosopher under the same lock as the death flag, so a death is detected once, reported once, and nothing is printed afterwards.
- Sleeping in short slices
- A custom sleep waits in 200 µs steps and checks the death flag between them. It is much more precise than a single long usleep and lets a sleeping philosopher stop as soon as the simulation ends.
- Printing guarded by the death flag
- State messages are printed only while holding the lock that protects the death flag, so output stays ordered and no line can slip out after a death is announced.
- Processes and semaphores for the bonus
- Running philosophers as processes isolates their memory, and a counting semaphore models a pile of forks in the middle of the table, which removes any fork-ordering problem.
Technical challenges
- Reporting a death within 10 ms while the monitor shares a lock with every philosopher
- Handling a single philosopher, who can only ever hold one fork and must die without deadlocking
- Stopping every thread or process cleanly at the end and releasing all mutexes, semaphores, and memory
- Accessing every shared value under a lock, so the program is free of data races rather than merely working most of the time
Results
- No deadlock or starvation under reasonable timings, such as five philosophers with 800 ms to die and 200 ms to eat and sleep
- Deaths are reported precisely, with no output after the simulation ends
- An optional stop condition once every philosopher has eaten the required number of times
- Two complete implementations: threads with mutexes, and processes with semaphores
Related case studies
Unix & Systems Programming
Minishell: Bash-like Unix shell in C
A Bash-like Unix shell in C with pipes, redirections, here-docs, variable expansion, built-ins, && and || operators, subshells, and wildcards.
Graphics & Visualization
FdF: 3D wireframe terrain renderer in C
An interactive 3D wireframe renderer in C that turns height maps into isometric landscapes, with rotation, zoom, panning, and color gradients.
C++ & Networking
Webserv: HTTP/1.1 web server in C++98
An HTTP/1.1 web server written from scratch in C++98, with NGINX-style configuration, virtual hosts, CGI, uploads, and non-blocking I/O.