Philosophers is a classic computer science problem that demonstrates the challenges of concurrent programming and synchronization. The project simulates the dining philosophers problem, where philosophers sit around a table with forks between them, and they must eat, think, and sleep while avoiding deadlocks and starvation.
The objective of this project is to learn about threading, mutexes, and synchronization mechanisms in concurrent programming. It teaches how to handle shared resources safely and efficiently while preventing race conditions and deadlocks.
The main components include:
- Main Program: Initializes the simulation and creates philosopher threads
- Philosopher Routines: Each philosopher follows a cycle of eating, sleeping, and thinking
- Monitoring System: Tracks philosopher states and detects death conditions
- Synchronization: Uses mutexes to prevent race conditions and ensure thread safety
Each philosopher is represented by a separate thread, and forks are shared resources protected by mutexes.
For a detailed breakdown of how the project works, please visit the documentation link below:
To clone this repository and compile the project, run the following commands:
git clone https://github.com/pin3dev/42_Philosophers.git
cd 42_Philosophers/philoThis will download the project to your local machine. Once inside the philo directory, run the provided Makefile to compile the project.
The project comes with a Makefile to automate the compilation process. The Makefile includes the following rules:
all: Compiles the philosopher program.clean: Removes object files from theobj/directory.fclean: Removes object files, the executable, and theobj/directory.re: Recompiles the entire project from scratch.
To compile the project, run:
makeThis will generate the executable philo.
The program takes the following arguments:
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]Parameters:
number_of_philosophers: Number of philosophers (and forks)time_to_die: Time in milliseconds before a philosopher dies if they haven't eatentime_to_eat: Time in milliseconds it takes for a philosopher to eattime_to_sleep: Time in milliseconds a philosopher spends sleepingnumber_of_times_each_philosopher_must_eat: (Optional) Number of times each philosopher must eat before the simulation ends
Examples:
# 5 philosophers, die after 800ms, eat for 200ms, sleep for 200ms
./philo 5 800 200 200
# Same as above, but simulation ends when each philosopher has eaten 7 times
./philo 5 800 200 200 7
# Classic scenario with 4 philosophers
./philo 4 410 200 200The program outputs the state changes of each philosopher:
timestamp_in_ms X has taken a forktimestamp_in_ms X is eatingtimestamp_in_ms X is sleepingtimestamp_in_ms X is thinkingtimestamp_in_ms X died
Where X is the philosopher number and timestamp_in_ms is the time since the simulation started.
This project follows the 42 School Norm guidelines. Certain decisions in the implementation may seem unconventional, but they were necessary to comply with the strict coding standards enforced by the school.
All the theoretical material used to study and carry out this project is organized in the tags described at the beginning of this README. In addition, these materials can be accessed directly via the link provided below.