Skip to content
/ philo Public

Eat, Sleep, Spaghetti, repeat. This project is about learning how threads work by precisely timing a group of philosophers on when to pick up forks and eat spaghetti without dying from hunger.

Notifications You must be signed in to change notification settings

Aggyx/philo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Philosophers

The Dining philosophers problem

This project shows the notions of execution threads in a program, and the sharing of resources between different threads using mutexes. Deadlock and data race problems will have to be dealt with.


To create this version of Edsger Dijkstra's problem I used two structures, One for the Main thread where the forks are located (variables shared by different Threads) and one structure for the scope of each routine executed by pthread_create() inside the main thread. Once the variables have been initialised by the parameter input, the pair philos will have a delay in coordinating the taking of the forks. So the odd philosophers will try to pick up the forks, eat, put the forks down, sleep and then think. Concurrently (thanks to the mutex), the even-numbered philosophers will do the same. The main thread (which launched all the philosophers) continues execution until it reaches pthread_join, except that this instance will be used to monitor the philosophers, so before the pthread_join the main thread will be in a continuous loop checking (with the mutex since the philo is also accessing its variables) the death variables, the number of times a philo has eaten and whether the philo is starving by looking at the time variable.

Installation

git clone git@github.com:Aggyx/philo
cd philo/
make

Usage

./philo philo_number time_to_die time_to_eat time_to_sleep [number_of_meals]

Note

All times passed as arguments should be in milliseconds, that way you won't have to wait ages to see the output :). In the example below, No one shoud die (see below):

./philo 2 401 200 200 [5]

Thanks !

Thanks to @PGCL1 to answer every question i had. Thanks to CodeVault for his amazing C channel.

About

Eat, Sleep, Spaghetti, repeat. This project is about learning how threads work by precisely timing a group of philosophers on when to pick up forks and eat spaghetti without dying from hunger.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published