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

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

The project of 42Berlin school. Learning the basics of threading, deadlock and data race.
https://github.com/maripeshko/42_philosophers

deadlock

Last synced: about 2 months ago
JSON representation

The project of 42Berlin school. Learning the basics of threading, deadlock and data race.

Awesome Lists containing this project

README

          

# 42_Philosophers

### "The unexamined life is not worth living."
— Socrates

42Berlin school project created by Maryna Peshko (mpeshko) in November of 2024. Studying the basics of multithreading. More details in the topic "Philosophers.subject.pdf"

### Logic

The two most important functions to understand a logic:
* `void *routine(void *arg)`
* `int ft_fork(t_philo *philo)`
* `int ft_think(t_philo *philo)`


In routine() all even philosophers start later than all odd ones.
```
if (philo->philo_id % 2 == 0)
ft_usleep(5);
```
`ft_fork()` has diferrent logic in taking forks for even and odd philosophers.

`ft_think()` is used to delay a philosopher before he takes a fork, so that he doesn't get there early and steal it from another philosopher who waited longer. This prevents some philosopher from waiting too long for his fork and dying.

### The program input:
`./philo`

` [number_of_philosophers]`

` [time_to_die]`

` [time_to_eat]`

` [time_to_sleep]`

` _(optional argument)`_

` [number_of_times_each_philosopher_must_eat]`


According to the subject, all tests are limited to 200 philosophers, and the minimum time is 60 ms.

### Examples

Philosopher should not eat and should die.

`./philo 1 800 200 200`



No Philosopher should die.

`./philo 5 800 200 200`



No Philosopher should die and the simulation should stop when every philosopher has eaten at least 7 times.

`./philo 5 800 200 200 3`



No Philosopher should die.

`./philo 4 410 200 200`



One Philosopher should die.

`./philo 4 299 200 100`



One Philosopher should die.

`./philo 10 299 2000 100`



No one should die

`./philo 10 299 2000 100`



Parsing tests

`./philo 2 "" "" ""`

`./philo -2 100 100 100`

`./philo f 100 100 100`

### How to check for data races
`valgrind --tool=helgrind`

### How to check used function

`nm -u ./philo`

### Contacts
https://www.linkedin.com/in/maryna-peshko/

email: marunapeshko@gmail.com

(c) Maryna Peshko