The Philosophers project, part of the 42 curriculum, is a classic synchronization problem designed to teach the fundamentals of concurrent programming and resource sharing. The project simulates a scenario where philosophers sit around a table, each alternating between thinking and eating. The challenge lies in managing the limited resources (forks) and preventing deadlock and starvation.
- Concurrency Handling: Implements multiple threads to simulate philosophers' activities.
- Synchronization: Utilizes mutexes and/or semaphores to manage access to shared resources.
- Deadlock Prevention: Incorporates strategies to prevent deadlock and ensure that each philosopher gets a chance to eat.
- Thread Safety: Ensures safe and efficient handling of concurrent operations.
-
Threads: Each philosopher is represented by a separate thread, running independently.
-
Mutexes: Used to control access to forks and ensure that no two philosophers can hold the same fork simultaneously.
-
State Management: Philosophers' states (thinking, hungry, eating) are carefully managed to avoid race conditions.
To build and run the Philosophers project, follow these steps:
- Clone the repository:
- git clone 'git@github.com:akeryan/philosophers.git'
- cd philosophers
- Compile the project:
- make
- Run the simulation:
- ./philo
- Clone the repository:
Upon running the simulation, you will see the philosophers alternating between thinking and eating, with proper synchronization ensuring that no two philosophers use the same fork simultaneously.