A multithreaded simulation of the Dining Philosophers Problem in C, built as part of the 42 School curriculum. This project explores concurrency, synchronization, and deadlock prevention using threads and mutexes.
- Simulates multiple philosophers sitting at a table
- Philosophers can think, eat, and sleep in cycles
- Uses mutexes to control access to forks
- Prevents deadlocks and starvation
- Time tracking for precise action simulation
- Command-line arguments for number of philosophers, times, and cycles
- C – Core language
- POSIX Threads (pthread) – For multithreading
- Mutexes – For synchronization
- Makefile – Build and manage project
- Clone the repository:
git clone https://github.com/Felipp3san/42-philosophers
cd 42-philosophers- Compile the project:
make- Run the executable with arguments:
./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 200 7-
Arguments:
number_of_philosophers– total philosophers at the tabletime_to_die– max time (ms) before a philosopher dies without eatingtime_to_eat– time (ms) a philosopher spends eatingtime_to_sleep– time (ms) a philosopher spends sleepingnumber_of_times_each_philosopher_must_eat(optional) – how many times each philosopher must eat
-
Observe philosopher actions: thinking, taking forks, eating, sleeping
-
Program terminates when all philosophers have eaten enough or a philosopher dies
- Build the executable:
make- Remove object files:
make clean- Remove all binaries and rebuild:
make re.
├── include/ # Header files
├── src/ # Source files
└── build/ # Object files (created after compilation)
Educational project under 42 School academic policy.