In computer science, The Dining Philosopher Problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.
Philosophers is an individual project at 42 that requires us to solve The Dining Philosopher Problem problem using threads, processes, mutex and semaphores. The mandatory part must be solved using threads and mutex. The solution was implemented using a monitoring thread, which checks if any philosopher has died and warns others to stop (locking mutex). The bonus consists of using processes instead of threads and semaphores instead of mutex, to solve it the solution used was to implement the death check between the sleeping times, avoiding the problem of reporting the death of a philosopher with more than 10ms.
The problem was proposed by Edsger W. Dijkstra in 1965 and is considered one of the classic problems about operating systems.
The problem consists of five philosophers sitting around a circular dinner table, where each philosopher has a plate for eating spaghetti and a fork to his right. To eat they need 2 forks but there is only one fork between each pair of plates. A philosopher has three actions: eating, thinking, or sleeping. When a philosopher gets hungry he will try to take the fork to his right and to his left, one at a time. If he manages to get both forks, he will eat the food at a certain time and will put the forks on the table, going to sleep and then going to think again.
Mandatory vs Bonus
- Unix logic
- Rigor
- Imperative programming
- Unix
Follow the steps below
# Clone the project and access the folder
git clone https://github.com/wwwwelton/philosophers && cd philosophers/
# Access the folder philo for the program that uses threads and mutex
cd philo
# or
# Access the folder philo_bonus for the program that uses processes and semaphores
cd philo_bonus
# Run make so you can build the program
make
# Run the program with the example arguments
./philo 5 800 200 200 10
# or
# Run the program with the example arguments for the bonus part
./philo_bonus 5 800 200 200 10
# Clean output objects with
make fclean
# Well done!
All arguments working with mandatory and bonus
#1 | #2 | #3 | #4 | #5 |
---|---|---|---|---|
5 | 800 | 200 | 200 | 10 |
number_of_philosophers | time_to_die | time_to_eat | time_to_sleep | *number_of_times_each_must_eat |
*The last argument, number_of_times_each_must_eat is optional. |
The project is regularly updated with bug fixes and code optimization.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Made by Welton Leite 👋 See my linkedin