Who thought treading on parallelism & concurrency land could be so philosophically deadly 🕱
- About 📌
philosophers
Problem 🜎philosophers
Requirements Overview ✅philosophers
Mandatory Implementation 📜philosophers
Bonus Implementation 📜- Usage 🏁
- Tests & Debug 🧪
- References 📚
- Documentation
- Research Papers
- Tools
- Articles
- License 📖
This is a classic CS exercise conceived as an introduction to the basics of threading a process. It gives an introductory glance into the difficulties contingent to the non-deterministic nature of multi-threaded applications.
-
One or more philosophers sit at a round table. There is a large bowl of spaghetti in the middle of the table.
-
The philosophers alternate between eating, thinking, or sleeping:
- While they are eating, they are not thinking nor sleeping;
- while thinking, they are not eating nor sleeping;
- While sleeping, they are not eating nor thinking.
-
There are also forks on the table. There are as many forks as philosophers.
-
A philosopher must take two forks to eat, one in each hand.
-
When finished eating, they put their forks back on the table and start sleeping.
-
Once awake, they start thinking again.
-
The simulation stops when a philosopher dies of starvation.
-
Every philosopher needs to eat and should never starve.
-
Philosophers don’t speak with each other.
-
Philosophers don’t know if another philosopher is about to die.
-
The program must not include any global variables.
-
It should accept the following command line arguments (as positive integers):
./philo <n_philos> <time_to_die> <time_to_eat> <time_to_sleep> [max_meals]
-
n_philos
also the number of forks. -
time_to_die
the time to die in milliseconds (length of philo's life). -
time_to_eat
the time to eat in milliseconds (length of time to hold forks). -
time_to_sleep
the time to sleep in milliseconds (length of time to rest). -
max_meals
the maximum number of meals for the simulation (optional argument). -
Each philosopher is numbered from 1 to
n_philos
. -
Philosophers number 1 should be sitting next to philosopher
n_philos
and philosophers number 2.
Regarding logs, the program should report any state change of a philosopher formatted as:
-
timestamp_in_ms <philo_n> has taken a fork.
-
timestamp_in_ms <philo_n> is eating.
-
timestamp_in_ms <philo_n> is sleeping.
-
timestamp_in_ms <philo_n> is thinking.
-
timestamp_in_ms <philo_n> has died.
-
Messages should not mix with each other.
-
A message announcing the end of the simulation should be displayed no more than 10ms after the time of death of the philosopher.
-
Each philosopher should be represented by a thread.
-
There is a fork between each pair of philosophers.
-
If there is only one philosopher, there should be only one fork on the table.
-
Each forks must be pretended by a mutex.
-
All forks are placed in the middle of the table.
-
They have no states in memory. The number of available forks is represented by a semaphore.
-
Each philosopher should be represented by a process, but the main process should not be a philosopher
Important
The code for both implementations is documented with doxygen comments. Enjoy!
Clone the repository and cd into either the mandatory (philo
) or bonus implementation (philo_bonus
):
git clone https://github.com/PedroZappa/42_philosophers.git 42_philosophers_passunca
# For the mandatory (Threads implementation w/ mutexes)
cd 42_philosophers_passunca/philo
# For the bonus (Processes implementation w/ semaphores)
cd 42_philosophers_passunca/philo_bonus
Build the program:
# For the mandatory
make
Run the program:
./philo 5 800 200 200
# or with a set max number of meals
./philo 5 800 200 200 7
Note
To execute the bonus implementation, you will need to instead call ./philo_bonus
command.
Run the following command and look at the Test Rules 🧪
& Debug Rules
to get a comprehensive list of all available test/debug commands:
make help
Note
If you use tmux
you are in for treat 😏
- GDB | Documentation
- ThreadSanitizerCppManual · google/sanitizers Wiki · GitHub
- Helgrind: a thread error detector | Documentation
- Instrumentation Options (Using the GNU Compiler Collection (GCC))
- Pthreads and OpenMP
- Heuristics for Efficient Dynamic Verification of Message Passing Interface And Thread Programs
- Pthreads for Dynamic and Irregular Parallelism
- philosophers-visualizer
- GitHub - SimonCROS/philosophers-visualizer CLI
- GitHub - mpdn/unthread: A deterministic, fuzzable pthread implementation
- rr: lightweight recording & deterministic debugging
- Philosophers 42 Guide— “The Dining Philosophers Problem” | by Dean Ruina | Medium
- The dining philoshophers (an introduction to multitasking) a 42 The Network project | by MannBell | Medium
This work is published under the terms of 42 Unlicense.