Skip to content

Guiribei/dining-philosophers-problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

Philosophers

Summary

The dining philosophers problem is a classic synchronization problem where multiple philosophers sit around a table with a bowl of spaghetti in the center. Each philosopher has a fork between themselves and the adjacent philosopher. A philosopher can only eat when they have both the left and right forks. They can either think, sleep, or eat at any given time.

Description

The challenge is to ensure that all philosophers get a fair chance to eat (all threads execute their routine) without causing deadlocks or resource starvation. For that, mutexes are used to protect against data racing. And it is necessary to handle them correctly to avoid deadlock.

Compilation

  1. Clone the repository:
git clone https://github.com/Guiribei/dining-philosophers-problem.git
  1. Change to the repository directory:
cd dining-philosophers-problem
  1. Compile the project using the provided Makefile:
make

Usage

The program takes 4 or 5 arguments:

  • The number of philosophers (and forks).
  • The time in milliseconds for a philosopher to die if they haven't started eating.
  • The time in milliseconds for a philosopher to eat.
  • The time in milliseconds for a philosopher to sleep.
  • (Optional) The number of times each philosopher must eat before the simulation ends.

Run the program as follows:

./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_to_eat]

Example:

./philo 5 800 200 200 10

This example will run the simulation with 5 philosophers, each having 800 ms to eat before they die. They will take 200 ms to eat, 200 ms to sleep, and must eat 10 times each before the simulation ends.

About

Philosophers 42-project about threading a process

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published