Skip to content

A Multithreaded Solution to the Dining Philosophers Problem

Notifications You must be signed in to change notification settings

hive-helsinki-projects/philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

A Multithreaded Solution to the Dining Philosophers Problem

score

solo estimated time XP earned

GitHub code size in bytes Code language count GitHub top language GitHub last commit

🍝 About The Project

philosophers is a multithreaded implementation of the classic Dining Philosophers problem.

Problem:

  • The Dining Philosophers problem is a classic concurrency problem dealing with synchronization.
  • It involves one or more philosophers sitting at a round table.
  • The philosophers alternatively eat, think, or sleep.
  • A fork is placed between each pair of adjacent philosophers. Each philosopher has one fork to the left and one fork to the right.
  • As spaghetti is difficult to serve and eat with a single fork, it is assumed that a philosopher must eat with two forks.
  • The challenge is to design a protocol that allows each philosopher to periodically stop thinking and eat without causing a deadlock.

Solution:

  • Each philosopher is represented by a separate thread.
  • The forks are represented by mutexes that control access to the shared resources (the forks).
  • A philosopher picks up the fork on their right, then the fork on their left, before they start eating.
  • After eating, a philosopher puts down both forks, then starts thinking.
  • The simulation stops when a philosopher dies or when each philosopher has eaten a certain number of times.

🏁 Getting Started

Prerequisites

  • gcc or clang
  • make

🛠️ Installation & Setup

  1. Clone the repository:
git clone https://github.com/lkilpela/philosophers.git
  1. Navigate into the project directory: cd philosophers/philo
  2. Compile the project: make

🚀 How to Use

  1. Run the program with five parameters:
  • num_of_philos: The number of philosophers.
  • time_to_die: The time in milliseconds a philosopher will die if they don't start eating.
  • time_to_eat: The time in milliseconds a philosopher spends eating.
  • time_to_sleep: The time in milliseconds a philosopher spends sleeping.
  • num_times_to_eat (optional): The number of times each philosopher must eat before the simulation ends.
  1. Example:
  • Do not test with more than 200 philosophers.
  • Do not test with time_to_die or time_to_eat or time_to_sleep set to values lower than 60 ms.
  • Test 1 800 200 200. The philosopher should not eat and should die.
  • Test 5 800 200 200. No philosopher should die.
  • Test 5 800 200 200 7. No philosopher should die and the simulation should stop when every philosopher has eaten at least 7 times.
  • Test 4 410 200 200. No philosopher should die.
  • Test 4 310 200 100. One philosopher should die.
  • Test with 2 philosophers and check the different times: a death delayed by more than 10 ms is unacceptable.
  • Test with any values of your choice to verify all the requirements. Ensure philosophers die at the right time, that they don't steal forks, and so forth.

📝 Evaluation Requirements

The philosophers project will be evaluated based on the following criteria:

  1. Correctness: The program should correctly implement the Dining Philosophers problem. No philosopher should be able to eat with only one fork, and no two philosophers should be able to use the same fork at the same time.

  2. Deadlock Prevention: The program should prevent deadlocks. At no point should the program get stuck because each philosopher is holding one fork and waiting for the other.

  3. Starvation Prevention: The program should prevent starvation. Each philosopher should be able to eat periodically, and no philosopher should die because they can't get access to the forks.

  4. Concurrency: The program should run the philosophers concurrently. Each philosopher should be represented by a separate thread.

  5. Output: The program should output the status of each philosopher (thinking, eating, sleeping, died) with timestamps in milliseconds.

  6. Code Quality: The code should be well-organized and easy to read. It should be free of memory leaks and undefined behavior.

Please note that these are general guidelines and the actual evaluation criteria may vary.

🧑‍💻 Peer Evaluations (3/3)

Here are some of the comments received during the peer evaluation of the philosophers project:

Peer 1: "..."

Peer 2: "..."

Peer 3: "..."

📜 License

This project is licensed under the MIT License.

About

A Multithreaded Solution to the Dining Philosophers Problem

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published