This project has been created as part of the 42 curriculum by .
The "Philosophers" project is a classic concurrency problem from 42 School, inspired by Edsger Dijkstra's "Dining Philosophers" thought experiment. The goal is to simulate philosophers sitting around a table, alternating between eating, thinking, and sleeping. Each philosopher needs two forks to eat, but only one fork is placed between each pair of philosophers. The challenge is to ensure that all philosophers get a chance to eat without causing deadlocks or starvation, using threads, mutexes, and semaphores to manage shared resources and synchronization42-cursus.gitbook.io+2. This project introduces students to threading, processes, and shared memory, focusing on avoiding race conditions, deadlocks, and ensuring proper resource management in a concurrent environment42-cursus.gitbook.io+2.
Philosophers must alternate between eating, sleeping, and thinking. Each philosopher must use two forks to eat. Forks are shared resources; only one philosopher can use a fork at a time. The simulation must stop if a philosopher dies (i.e., hasn't eaten within a specified time). The program must accept the following arguments:
- number_of_philosophers
- time_to_die (in milliseconds)
- time_to_eat (in milliseconds)
- time_to_sleep (in milliseconds)
- number_of_times_each_philosopher_must_eat (optional)
The program must output the status of each philosopher (e.g., "has taken a fork", "is eating", "is sleeping", "is thinking", "died").
Implement the solution using threads and mutexes. Ensure no data races or deadlocks occur. Philosophers must not starve; all must have a chance to eat if possible. The program must handle errors gracefully and exit cleanly.
42 Philosophers Project Guide
Dining Philosophers Problem Overview