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.

GitHub
0
0
0

Last updated

April 07, 2026

Philosophers

Philosophers is a project developed as part of the curriculum at 42 School, aimed at solving the classic dining philosophers problem in C with threads and mutexes. The program simulates philosophers who eat, think, and sleep while sharing forks, with constraints on timing and synchronization.

Usage

Prerequisites

Clone the repository to your local machine using the following command in the terminal.

sh
git clone https://github.com/milandekruijf/philosophers.git && cd philosophers

Compiling

In order to compile the program, run the following command in the project root directory.

sh
make

Compiles 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:

sh
make STRICT=0

To link with AddressSanitizer (useful when investigating memory issues), run:

sh
make TEST=1

Running

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.

sh
./out/philo 5 800 200 200
sh
./out/philo 5 800 200 200 7

Each 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:

sh
make clean
sh
make fclean

Rebuild from scratch:

sh
make re

Features

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.

Acknowledgements

  • 42: The educational institution that inspired and supported the development of this project.
  • Codam: The partner school of 42 in the Netherlands, where the project was developed.