An open API service indexing awesome lists of open source software.

https://github.com/pedrozappa/42_philosophers

42 Project : philosophers
https://github.com/pedrozappa/42_philosophers

42 42born2code 42cursus c makefile philosophers philosophers-dinner-problem philosophers42

Last synced: 3 months ago
JSON representation

42 Project : philosophers

Awesome Lists containing this project

README

        

42 Project : philosophers

# philosophers

> Who thought treading on parallelism & concurrency land could be so philosophically deadly πŸ•±







___

___

Table o'Contents

* [About πŸ“Œ](#about-)
* [`philosophers` Problem 🜎](#philosophers-problem-)
* [`philosophers` Requirements Overview βœ…](#philosophers-requirements-overview-)
* [`philosophers` Mandatory Implementation πŸ“œ](#philosophers--mandatory-implementation-)
* [`philosophers` Bonus Implementation πŸ“œ](#philosophers--bonus-implementation-)
* [Usage 🏁](#usage-)
* [Tests & Debug πŸ§ͺ](#tests--debug-)
* [References πŸ“š](#references-)
* [Documentation](#documentation)
* [Research Papers](#research-papers)
* [Tools](#tools)
* [Articles](#articles)
* [License πŸ“–](#license-)

# About πŸ“Œ

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.

## `philosophers` Problem 🜎

* **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.

## `philosophers` Requirements Overview βœ…

- The program must not include any global variables.

- It should accept the following command line arguments (as positive integers):
```sh
./philo [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 has taken a fork.`
- `timestamp_in_ms is eating.`
- `timestamp_in_ms is sleeping.`
- `timestamp_in_ms is thinking.`
- `timestamp_in_ms 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.

___
# `philosophers` Mandatory Implementation πŸ“œ

- 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.

___
# `philosophers` Bonus Implementation πŸ“œ

- 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!

___

# Usage 🏁

Clone the repository and cd into either the mandatory (`philo`) or bonus implementation (`philo_bonus`):
```sh
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:
```sh
# For the mandatory
make
```

Run the program:
```sh
./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.
___

# Tests & Debug πŸ§ͺ

Run the following command and look at the `Test Rules πŸ§ͺ` & `Debug Rules ο†ˆ` to get a comprehensive list of all available test/debug commands:
```sh
make help
```

> [!Note]
> If you use `tmux` you are in for treat 😏
___
# References πŸ“š

## Documentation

- [GDB | Documentation](https://sourceware.org/gdb/current/onlinedocs/gdb)
- [ThreadSanitizerCppManual Β· google/sanitizers Wiki Β· GitHub](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual)
- [Helgrind: a thread error detector | Documentation](https://valgrind.org/docs/manual/hg-manual.html)
- [Instrumentation Options (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)

## Research Papers

- [Pthreads and OpenMP](https://www.diva-portal.org/smash/get/diva2:944063/FULLTEXT02.pdf)
- [Heuristics for Efficient Dynamic Verification of Message Passing Interface And Thread Programs](https://core.ac.uk/download/pdf/276266676.pdf)
- [Pthreads for Dynamic and Irregular Parallelism](https://core.ac.uk/download/pdf/276266676.pdf)
___
## Tools

- [philosophers-visualizer](https://nafuka11.github.io/philosophers-visualizer/)
- [GitHub - SimonCROS/philosophers-visualizer CLI](https://github.com/SimonCROS/philosophers-visualizer)
- [GitHub - mpdn/unthread: A deterministic, fuzzable pthread implementation](https://github.com/mpdn/unthread)
- [rr: lightweight recording & deterministic debugging](https://rr-project.org/)
- [Introducing rr Chaos Mode](https://robert.ocallahan.org/2016/02/introducing-rr-chaos-mode.html)

## Articles

- [Philosophers 42 Guideβ€” β€œThe Dining Philosophers Problem” | by Dean Ruina | Medium](https://medium.com/@ruinadd/philosophers-42-guide-the-dining-philosophers-problem-893a24bc0fe2)
- [The dining philoshophers (an introduction to multitasking) a 42 The Network project | by MannBell | Medium](https://m4nnb3ll.medium.com/the-dining-philoshophers-an-introduction-to-multitasking-a-42-the-network-project-34e4141dbc49)
___

# License πŸ“–

This work is published under the terms of 42 Unlicense.

(get to top)