https://github.com/link-wolf/philosophers
42 project - Simultation of the Philosophers problem, with C-threads and mutex
https://github.com/link-wolf/philosophers
42 42born2code 42school c macos simulation
Last synced: about 2 months ago
JSON representation
42 project - Simultation of the Philosophers problem, with C-threads and mutex
- Host: GitHub
- URL: https://github.com/link-wolf/philosophers
- Owner: Link-Wolf
- Created: 2022-03-23T14:11:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-30T13:14:15.000Z (over 1 year ago)
- Last Synced: 2025-01-27T12:49:41.926Z (over 1 year ago)
- Topics: 42, 42born2code, 42school, c, macos, simulation
- Language: C
- Homepage:
- Size: 296 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Philosophers
I’ve never thought philosophy would be so deadly.
Learn the basics of threading a process, how to make threads, and discover the mutex
Report Bug
·
Request Feature
Table of Contents
## About The Project
The philosophers' problem is the classical problem of synchronization which says that `n` philosophers are sitting around a circular table and their job is to think and eat in turn.
A bowl of noodles is placed at the center of the table along with `n` forks between each philosopher
To eat a philosopher needs both their right and left forks. A philosopher can only eat if both their immediate left and immediate right forks are available
In case either of their immediate left or immediate right fork is not available then they put down their other fork and start thinking again.
If the philosopher can eat, they do then they'll sleep before thinking again
The goal of the project is to simulate this problem by using C `threads`, and with custom parameters
- `number_of_philosophers` : the number of philosophers and also the number of forks
- `time_to_die` : if a philosopher doesn’t start eating `time_to_die` milliseconds after starting his last meal or the beginning of the simulation, he dies
- `time_to_eat` : The time (in milliseconds) it takes for a philosopher to eat. During that time he will need to keep the two forks
- `time_to_sleep` : The time (in milliseconds) the philosopher will spend sleeping
- **[Optional]** `number_of_times_each_philosopher_must_eat` : if every philosopher eat at least `number_of_times_each_philosopher_must_eat` times, the simulation will stop. If not specified, the simulation will stop only at the death of a philosopher
Each philosopher should be a `thread`, to avoid philosophers duplicating forks we have to protect the forks state with a
`mutex` for each of them and philosophers don’t speak with each other.
Philosophers should avoid dying, so the program should organize each philosopher time, and print any change of status of a philosopher as :
```
[timestamp_in_ms] X ([has taken a fork] / [is eating] / [is sleeping] / [is thinking] / [died])
```
### Bonus features
In the bonus version, all the forks are in the middle of the table
They have no states in memory but the number of available forks is represented by
a `semaphore`
And each philosopher should be a process and the main process should not be a philosopher
## Getting Started
Because it's a simple C program, there isn't much to say here
### Prerequisites
Having a C compiler like cc, gcc or clang
### Installation
1. Clone the repo
```sh
git clone https://github.com/Link-Wolf/Philosophers.git
```
2. Compile Philosophers
```sh
cd Philosophers/philo; make
```
3. Execute the program with parameters described here and see how it works
```sh
./philo 6 800 400 200
```
_it should run forever_
Or stop the program with `number_of_times_each_philosopher_must_eat`
```sh
./philo 6 800 400 200 15
```
## Usage
Test this Philosophers with the parameters you want, with or without a stopper !
#### Example
```sh
./philo 3 800 400 200 3
```
#### Output
```
0 0 is thinking
0 0 has taken a fork
0 0 has taken a fork
0 0 is eating
0 1 is thinking
0 2 is thinking
400 0 is sleeping
400 1 has taken a fork
400 1 has taken a fork
400 1 is eating
600 0 is thinking
800 1 is sleeping
800 2 has taken a fork
800 2 has taken a fork
800 2 is eating
1000 1 is thinking
1200 2 is sleeping
1200 0 has taken a fork
1200 0 has taken a fork
1200 0 is eating
1400 2 is thinking
1600 0 is sleeping
1600 1 has taken a fork
1600 1 has taken a fork
1600 1 is eating
1800 0 is thinking
2000 1 is sleeping
2000 2 has taken a fork
2000 2 has taken a fork
2000 2 is eating
2200 1 is thinking
2400 2 is sleeping
2400 0 has taken a fork
2400 0 has taken a fork
2400 0 is eating
2600 2 is thinking
2800 0 is sleeping
2800 1 has taken a fork
2800 1 has taken a fork
2800 1 is eating
3000 0 is thinking
3200 1 is sleeping
3200 2 has taken a fork
3200 2 has taken a fork
3200 2 is eating
```
#### Example where a philosopher die
```sh
./philo 3 500 400 200
```
#### Output
```
0 0 is thinking
0 2 is thinking
0 0 has taken a fork
0 0 has taken a fork
0 0 is eating
0 1 is thinking
400 0 is sleeping
400 1 has taken a fork
400 1 has taken a fork
400 1 is eating
500 2 died
```
## Roadmap
- [ ] Add the bonuses (surely never)
See the [open issues](https://github.com/Link-Wolf/Philosophers/issues) for a full list of proposed features (and known issues).
## Contributing
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request