A synchronization and concurrency challenge from 42 School based on the Dining Philosophers Problem.
Philosophers is an individual project at 42 School that challenges students to solve the classic Dining Philosophers Problem. The task involves creating a simulation where multiple philosophers, seated at a circular table, alternate between thinking, eating, and sleeping, while sharing limited resources (forks). The main challenge is to ensure proper synchronization and avoid common pitfalls like deadlocks and starvation.
To implement a solution, I adhered to the specifications provided by the 42 project guidelines and used various strategies to coordinate philosopher actions efficiently.
- Concurrent Simulation: Each philosopher is modeled as a separate process or thread, demonstrating inter-process communication and synchronization.
- Deadlock Prevention: Implements strategies to avoid deadlocks and resource contention.
- Efficient Resource Management: Ensures timely access to forks while promoting fair resource distribution among all philosophers.
- Clear Status Updates: Logs each philosopher's actions (e.g., picking up forks, eating, sleeping, thinking) to help visualize the simulation progress.
- C — Core programming language.
- POSIX Threads or Processes — For creating concurrent simulations.
- Makefile — Used for building and automating the project.
Clone the repository and use the following commands in your terminal:
# Compile the project
make all
# Remove object files
make clean
# Remove object files and executable
make fclean
# Clean and recompile
make re
Run the executable with the required arguments. The program accepts parameters such as the number of philosophers, time to die, time to eat, time to sleep, and optionally the number of times each philosopher must eat.
Example usage:
./philo 5 800 200 200
Example with a meal limit:
./philo 5 800 200 200 7
The simulation was tested under multiple scenarios to ensure robustness and correctness:
- Validating with various numbers of philosophers.
- Monitoring behavior under high contention for forks.
- Confirming that no deadlocks or starvation occur during long runs.
- Checking the simulation output for proper chronological status updates of each philosopher.
Additional custom scripts were used to automate the testing and verify the expected results.
- Philosophers 42 Guide — "The Dining Philosophers Problem"
A comprehensive guide on implementing the solution. - YouTube Video
Detailed explanation and visual breakdown of the problem. - YouTube Playlist
A series of tutorials covering various aspects of the Dining Philosophers Problem and concurrent programming.
Here’s my score for the Philosophers project:
Feel free to reach out or contribute to this project on GitHub!