This repository contains the solution for the Philosophers project at Hive Helsinki. The Philosophers project is designed to teach students about synchronization techniques in a multithreaded environment using mutexes to protect the access to shared resources. In the bonus, instead of mutexes semaphores are required.
The project requires you to implement the "Dining Philosophers" problem. In this problem, a certain number of philosophers sit at a round table with a bowl of spaghetti in front of each philosopher. Philosophers alternate between thinking, eating and sleeping. To eat, a philosopher must pick up the two forks (one on the left and one on the right) of their place setting. Since there are only as many forks as there are philosophers, they must share forks and avoid deadlocks.
The goal is to create a program that simulates the behavior of philosophers, ensuring that they can do their actions without any conflicts, deadlocks, or starvation.
- Implements a simulation of the Dining Philosophers problem using threads.
- Manages the shared resources (forks) to avoid conflicts.
- Implements a time-limited simulation where philosophers think, pick up forks, eat, and sleep.
- Monitors and reports the status of the philosophers and their actions.
Follow these instructions to get the project up and running on your local machine.
Before you start, ensure you have the following prerequisites:
- A Linux-based system (Linux/Unix).
- GNU Make.
- GCC compiler
-
Clone this repository to your local machine:
git clone https://github.com/yourusername/hive-helsinki-philosophers.git
-
Compile the project:
make
-
Run the program with appropriate arguments:
./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [number_of_times_each_philosopher_must_eat]
Replace the placeholders in square brackets with your desired values.
The program will start running, simulating the behavior of philosophers. You can observe their actions, and the program will report the actions taken by the philosophers.
The problem statement largely follows the same rules as the mandatory part. However, in the bonusm instead of forks being positioned between two philosphers, a common pool of forks is at the centre of the table for everyone to reach.