Skip to content

Multithreaded solution in C to the classic Dining Philosophers problem using mutexes

License

Notifications You must be signed in to change notification settings

Maroua-netizen/Philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 

Repository files navigation

Philosophers

This project provides a multithreaded solution in C to the classic Dining Philosophers problem using mutexes.

Description

The Dining Philosophers problem is a classic synchronization problem involving philosophers who alternate between thinking and eating, but need to share limited resources (forks). This implementation uses pthreads and mutexes to prevent deadlocks and starvation.

Features

  • Written in C
  • Uses pthreads for multithreading
  • Employs mutexes for resource synchronization
  • Includes a Makefile for easy compilation

Getting Started

  1. Clone the repository:
    git clone https://github.com/Maroua-netizen/Philosophers.git
  2. Compile the project:
    make

How to Run

Run the program with the following command:

  ./philosophers [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [number_of_times_each_philosopher_must_eat (optional)]
  • number_of_philosophers: The total number of philosophers (and forks).
  • time_to_die: Time in milliseconds before a philosopher dies if they don't start eating.
  • time_to_eat: Time in milliseconds it takes for a philosopher to eat.
  • time_to_sleep: Time in milliseconds a philosopher spends sleeping.
  • number_of_times_each_philosopher_must_eat (optional): If specified, simulation stops when each philosopher has eaten this many times.

Example:

  ./philosophers 5 800 200 200

This command starts a simulation with 5 philosophers, where each will die if they don’t eat within 800ms, spend 200ms eating, and 200ms sleeping.

Example with the optional argument:

./philosophers 5 800 200 200 7

This runs the simulation as above, but it finishes when each philosopher has eaten 7 times.

License

This project is licensed under the MIT License.