Philosophers is a 42 project that tackles the classic dining philosophers problem using threads and mutexes. The real challenge is managing concurrent access to shared resources without causing deadlocks or data races. Each philosopher must think, eat, and sleep in their own thread while competing for forks with their neighbors β all without starving to death. Proper synchronization is critical, and timing precision determines whether philosophers live or die.
- Multi-threaded simulation with
pthread Mutex-based synchronization for shared resources (forks)- Deadlock prevention through careful resource ordering
- Precise timing with
usleep()andgettimeofday() - Real-time status logging (thinking, eating, sleeping, dying)
- Configurable parameters: number of philosophers, time to die/eat/sleep
git clone https://github.com/YourName/philosophers.git
cd philosophers
make
./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [optional: number_of_times_each_philosopher_must_eat]Example:
./philo 5 800 200 200
# 5 philosophers, 800ms to die, 200ms to eat, 200ms to sleep- Threading β
pthread_create(),pthread_join(),pthread_detach() - Mutexes β
pthread_mutex_lock(),pthread_mutex_unlock() - Race conditions β protecting shared data from concurrent access
- Deadlock avoidance β preventing circular wait conditions
- Timing precision β microsecond-level accuracy for survival checks