Dining philosophers simulation in C with POSIX threads and mutexes.
threads • mutexes • synchronization • concurrency • timing
Philosophers is a project developed as part of the 42 curriculum.
This repository contains the mandatory philo program, a simulation of the dining philosophers problem. Each philosopher is represented by a thread, each fork is represented by a mutex, and the main monitoring loop checks starvation and the optional meal target.
This project focuses on:
- thread creation and lifecycle management
- synchronization of shared state with mutexes
- starvation detection based on timestamps
- input validation and clean resource handling
- argument validation with overflow and format checks
- dedicated handling for the single philosopher case
- one thread per philosopher and one mutex per fork
- synchronized status printing to avoid mixed terminal output
- death monitoring and optional meal limit support
- current repository only contains the mandatory thread-based version, not the bonus semaphore/process version
Through this project, the following concepts are exercised:
- POSIX threads (
pthread_create,pthread_join) - mutex locking and shared resource protection
- concurrent access to shared state
- time handling in milliseconds with
gettimeofday - parsing and validating command-line arguments
- manual cleanup of allocated memory and mutexes
Clone the repository and compile the project:
git clone git@github.com:Middle-555/Philosophers.git
cd Philosophers
makeAvailable Makefile rules:
make
make clean
make fclean
make reThe project is compiled with cc and the flags -Wall -Werror -Wextra -g -pthread.
Run the program with:
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]number_of_philosophers: number of philosopher threads to createtime_to_die: maximum time in milliseconds a philosopher can stay without eatingtime_to_eat: time in milliseconds spent eatingtime_to_sleep: time in milliseconds spent sleepingnumber_of_times_each_philosopher_must_eat: optional stop condition for the simulation
./philo 1 800 200 200
./philo 2 800 200 200 2
./philo 5 800 200 200 3All time values are interpreted in milliseconds. When the optional fifth argument is provided, the simulation stops once every philosopher has reached the required meal count.
.
├── include/
│ └── philo.h
├── src/
│ ├── checking_error.c
│ ├── data.c
│ ├── handle_simulation.c
│ ├── main.c
│ ├── philo_death.c
│ ├── philo_routine.c
│ ├── utils.c
│ └── utils2.c
├── makefile
└── README.md
include/: shared structures, typedefs, and function prototypessrc/: argument parsing, initialization, philosopher routine, monitoring, and utility helperssrc/checking_error.c: input validation and special handling for the one philosopher casesrc/data.c: data initialization, mutex setup, and simulation startupsrc/philo_routine.c: philosopher lifecycle, eating, sleeping, thinking, and waiting logicsrc/philo_death.c: starvation detection and optional meal-limit completion check
This implementation currently targets the mandatory philo program:
- written in C
- built with
cc - synchronized with POSIX threads and mutexes
- based on millisecond timing
- organized around a single executable:
philo
This repository does not include the bonus version based on processes and semaphores.
The following checks were verified locally on this repository:
- successful compilation with
make - invalid argument rejection
- dedicated one philosopher behavior
- bounded simulation runs with the optional meal limit
- terminal output consistency during concurrent execution
./philo 0 800 200 200
./philo 1 800 200 200
./philo 2 800 200 200 2
./philo 5 800 200 200 3If valgrind is available on your machine, you can use:
valgrind --leak-check=full --show-leak-kinds=all ./philo 4 410 200 200 2No dedicated automated test suite is present in this repository at the moment.
This project is a good exercise to improve in the following areas:
- reasoning about concurrent execution
- protecting shared state with a minimal locking strategy
- debugging timing-sensitive behavior
- structuring a small multi-file C project
Although the current repository provides a working mandatory version, several improvements could be considered:
- add the bonus implementation with processes and semaphores
- add automated test and sanitizer targets in the
makefile - refine logging and monitoring to make debugging easier
- add dedicated tooling for memory and race-condition analysis
Kpourcel 42 student