Philosophers are sitting around a table where there is a large bowl of spaghetti in the middle. They all bring their own fork. They can alternatively eat, sleep or think. But in order to eat, they need to use 2 forks.
In a multi-threading manner, you gonna find a way to prevent any philosopher from dying. Read more here
TEST | How philosopher should react |
---|---|
./philo 1 800 200 200 |
The philosopher should not eat and should die! |
./philo 5 800 200 200 |
No one should die! |
./philo 5 800 200 200 7 |
No one should die and the simulation should stop when all the philosophers has eaten at least 7 times each |
./philo 4 410 200 200 |
No one should die! |
./philo 4 310 200 100 |
A philosopher should die! |
./philo 5 800 200 150 |
No one should die! |
./philo 3 610 200 80 |
No one should die! |
If for the last 2 tests, you have philo dying, it means that you are still missing something in order to solve the dinning philosopher problem. How to solve it ?
void think(t_philo *philo)
{
display(philo, "is thinking");
// We create a time_to_think delay, that prevent the current philo to take forks
// and eat directly after finishing to sleep.
// formula: ((time_to_die - (time_to_eat + time_to_sleep)) / 2) * 1000;
usleep(((philo->data->time_to_die - (philo->data->time_to_eat \
+ philo->data->time_to_sleep)) / 2) * 1000);
}
Philosophers should now have stop to steal forks to each other :) (Drop a star if that helped you)
Compile using:
make
Then start a simulation using:
./philo <nb_philo> <time_to_die> <time_to_eat> <time_to_sleep> <nb_of_time_each_philo_must_eat> //last argument is facultative
To check data races, go into your Makefile and use -fsanitize=thread when you compile. You can either check them using:
valgrind --tool=helgrind ./philo arg1 arg2 arg3 arg4