This project demonstrates my understanding of multithreading, semaphores, and shared memory in C by implementing the classic Dining Philosophers problem.
The Dining Philosophers problem is a classic synchronization problem involving a certain number of philosophers who alternate between thinking and eating. They use shared forks (one between each pair of philosophers), and the challenge is to design a system where no philosopher will starve and no deadlock will occur.
- Multithreading: Each philosopher is represented as a separate thread.
- Semaphores: Used to control access to shared forks to prevent race conditions.
- Shared Memory: Utilized for sharing semaphore states among the philosopher threads.
dining_philosophers_shared.c
: Main source code implementing the problem.Makefile
: Build script to compile and run the program.compile_run.sh
: Bash script for compiling and running the program.
-
Compile the program:
make
-
Run the program:
make run
-
Clean the build files:
make clean
-
Give execution permission to the script:
chmod +x compile_run.sh
-
Run the script:
./run_dining_philosophers.sh
- GCC compiler
- POSIX-compliant system
- Multithreading: Demonstrates creating and managing multiple threads using
pthread
library. - Semaphores: Illustrates the use of POSIX semaphores for synchronization.
- Shared Memory: Shows how to create and manage shared memory using
shm_open
,mmap
, andshm_unlink
.
This project is licensed under the MIT License.