Implementation of the classic dining philosophers problem using POSIX threads and mutexes.
philosophers simulates N philosophers sitting at a round table, each alternating between thinking, picking up forks, eating, and putting down forks. The challenge is to prevent deadlocks and starvation while correctly detecting when a philosopher has died.
- Each philosopher is a thread
- Each fork is a mutex shared between two adjacent philosophers
- A philosopher dies if they have not eaten within
time_to_diemilliseconds - The simulation stops when one philosopher dies, or when all have eaten
number_of_timestimes (optional)
make
./philo <number_of_philosophers> <time_to_die> <time_to_eat> <time_to_sleep> [number_of_times_each_philosopher_must_eat]Example:
./philo 5 800 200 200main → parse args → init forks (mutexes) → init philosophers (threads)
→ each thread: loop { think → lock forks → eat → unlock → sleep }
→ monitor thread: check timestamps → detect death → stop all
- Language: C
- Key concepts: POSIX threads (
pthread_create,pthread_join), mutexes (pthread_mutex_t), race conditions, deadlock prevention
| Field | Value |
|---|---|
| Project | Philosophers |
| Circle | 3 |
| Norminette | Compliant |
- Thread lifecycle management with
pthread_createandpthread_join - Mutex locking strategies to avoid deadlocks
- Precise timing with
gettimeofdayandusleep - Detecting race conditions and data races