This is a project for 42Heilbronn school's curriculum. The goal of the project is to write a program simulating a group of philosophers who sit at a round table and alternately eat, think, or sleep.
- One or more philosophers sit at a round table. There is a large bowl of spaghetti in the middle of the table.
- The philosophers alternatively eat, think, or sleep.
- There are as many forks as philosophers.
- A philosopher needs two forks to eat spagetti.
- When a philosopher has finished eating, they put their forks back on the table and start sleeping. Once awake, they start thinking again. The simulation stops when a philosopher dies of starvation.
- Every philosopher needs to eat and should never starve.
- Philosophers don’t speak with each other.
- Philosophers don’t know if another philosopher is about to die.
- No need to say that philosophers should avoid dying!
run make
or make_bonus
The program will be executed as follows:
./philosophers num_of_philosopers time_to_die time_to_eat time_to_sleep [num_of_times_each_philo_must_eat]
- time_to_die (in milliseconds): If a philosopher didn’t start eating time_to_die milliseconds since the beginning of their last meal or the beginning of the simulation, they die.
- time_to_eat (in milliseconds): The time it takes for a philosopher to eat. During that time, they will need to hold two forks.
- time_to_sleep (in milliseconds): The time a philosopher will spend sleeping.
- number_of_times_each_philosopher_must_eat (optional argument): If all philosophers have eaten at least this much times the simulation stops
i.e:
$>./philosophers 4 800 200 200 5
To visualize the behavior of the philosopers, just insert the output in this Visualizer. The times shouldn't be smaller than 60 and there shouldn't be more than 200 philosphers.
- Each of the philosophers is a thread.
- There is the same amount of forks as there are philosophers.
- Each philosopher has a fork on their left and their right side.
- The forks state should be protected using mutexes.
- Each of the philosophers is a process.
- There is the same amount of forks as there are philosophers.
- The forks are placed in the middle of the table.
- They have no states in memory but the number of available forks is represented by a semaphore.
- how to create and use
threads
with the <pthread.h> - using
mutexes
andsemaphores
, to prevent data races. - preventing
deadlocks
andlivelocks