Skip to content

JorFik/Philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

(The Project is still under construction, keep up with the updates for its completion (including the Bonus part) ;])

Philosophers

Philosophers is a multithreaded dining philosophers problem simulator. The problem is a classic multi-process synchronization problem which was originally formulated in 1965 by Edsger Dijkstra.

Problem Statement

There are N philosophers sitting around a round table. Each philosopher has a dish of spaghetti. A fork is placed between each pair of adjacent philosophers, and each philosopher must alternately think, eat and sleep. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it's not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but can't start eating before getting both forks.

For more information, see Dining Philosophers Problem.

Specifications

  1. The program takes in the following arguments:

    • N - the number of philosophers
    • die_time - time to die (in milliseconds). If a philosopher doesn’t start eating die_time milliseconds after starting his last meal or the beginning of the simulation, it dies
    • eat_time - time the philosopher will need to eat (in milliseconds). During that time they will need to keep the two forks
    • sleep_time - time the philosopher will need to sleep (in milliseconds)
    • meal_count - (optional) number of times each philosopher must eat. If all philosophers eat at least meal_count times, the simulation will stop
  2. The philosophers are numbered from 1 to N.

  3. The simulation uses threads and mutexes.*

  4. Each philosopher should be a thread.*

  5. Each fork is represented by a mutex.*

** = This specifications are only for the mandatory part of the project, for more information about the bonus part see BONUS.

Usage

To compile the program, run:

make -C philo

To start the simulation, run:

./philo/philo N die_time eat_time sleep_time [meal_count]

Substitute N, die_time, eat_time, sleep_time, and meal_count(optional) with the desired values.

Output

The program provides the following output** for each philosopher:

  • When a philosopher takes a fork
  • When a philosopher is eating
  • When a philosopher is sleeping
  • When a philosopher is thinking
  • When a philosopher dies

** = Each output is preceded by a timestamp (in milliseconds).

Note

This project is part of the curriculum for the 42 School. It's designed to help students learn about multithreading and synchronization in C.

License

This project is licensed under the MIT License. This means you can use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the final products. However, you need to include this license in any substantial uses of the work. For more details, please see the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published