Skip to content

Solution for the well known dinning philosopher problem. Additionnal tricky tests and explanation on how to fix the time_to_think formula.

Notifications You must be signed in to change notification settings

ethan0905/philosopher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 

Repository files navigation

philosopher 👥 esafar's 42 Philosophers Score

jcluzet

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

📔 Summary

❗️ Tricky tests

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)

⚙️ How to run the project ?

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

🗃️ Data races

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

About

Solution for the well known dinning philosopher problem. Additionnal tricky tests and explanation on how to fix the time_to_think formula.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published