Skip to content

cantaskin/philosophers

Repository files navigation

Philosophers

The dining philosophers problem is a classic example used in concurrent algorithm design to demonstrate synchronization problems and techniques for solving them in computer science. It was first formulated as a student exam exercise by Edsger Dijkstra in 1965 and later modified by Tony Hoare.

philos

The problem involves N philosophers seated at a round table, each with a plate of spaghetti that requires two forks to eat. The forks are positioned between each philosopher and only two adjacent philosophers can use them at the same time. The problem is to design a concurrent algorithm where no philosopher starves to death, meaning each philosopher can continuously alternate between eating, thinking, and sleeping, without knowing when the other philosophers will want to eat or think. Each philosopher should be represented as a thread, and if there are multiple philosophers, each should have a fork on their left and right.Thus, the two chopsticks can only be used if their nearest two neighbors are not eating. An individual philosopher will leave both chopsticks after finishing eating. The problem is to design a simultaneous algorithm in which no philosopher will die of hunger, that is, assuming that no philosopher can know when the others may want to eat or think, each one can continue to go back and forth between eating and thinking forever.

The rules are:

• One or more philosophers are sitting around a round table. In the center of the table, there is a big bowl of spaghetti.
• Philosophers alternatively eat, think or sleep. They can only do one at a time.
• There are forks on the table, as many as philosophers.
• A philosopher takes one fork in each hand to eat.
• When the philosopher finishes eating, they put the forks back on the table and go to sleep. When they wake up, they start thinking again.
• If a philosopher dies of hunger, the simulation stops.
• Each philosopher needs to eat and should never be hungry.
• Philosophers do not know if another philosopher is about to die.

The rules are as follows:

• Each philosopher must be a thread.
• If there are more than one philosopher, each philosopher has a fork on their left and a fork on their right.
• If there is only one philosopher, there should only be one fork on the table.
• To prevent philosophers from duplicating forks, protect the status of the forks with mutexs.

basic_img

Arguments

(./philo philo_count time_to_die eating_time sleeping_time eat_destination) (./philo 10 800 310 200) (./philo 10 800 310 200 5)

Definitions
• philo_count: Specifies the number of philosophers.
• time_to_die: The time a philosopher dies of starvation.
• eating_time: The time a philosopher spends eating.
• sleeping_time: The time a philosopher spends sleeping after eating.
• eat_destination: The destination of each philosopher's meal (optional)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages