Certainly! Below is a C program that simulates the Dining Philosophers Problem using processes and semaphores for synchronization. The program creates 5 child processes (philosophers) from a parent process and uses semaphores to represent the forks that the philosophers need to pick up to eat.
C Program: Dining Philosophers Problem
Make sure you have the required libraries and compile the code with a C compiler (e.g., gcc).
Here’s the complete program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#define NUM_PHILOSOPHERS 5
// Semaphores for forks
sem_t forks[NUM_PHILOSOPHERS];
void think(int philosopher_id) {
printf("Philosopher %d is thinking...\n", philosopher_id);
sleep(rand() % 2 + 1); // Simulate thinking
}
void eat(int philosopher_id) {
printf("Philosopher %d is eating...\n", philosopher_id);
sleep(rand() % 2 + 1); // Simulate eating
}
void philosopher(int id) {
while (1) {
think(id);
// Pick up right fork
sem_wait(&forks[id]);
// Pick up left fork
sem_wait(&forks[(id + 1) % NUM_PHILOSOPHERS]);
eat(id);
// Put down left fork
sem_post(&forks[(id + 1) % NUM_PHILOSOPHERS]);
// Put down right fork
sem_post(&forks[id]);
}
}
int main() {
// Initialize random number generator
srand(time(NULL));
// Initialize semaphores for forks
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&forks[i], 0, 1); // Each fork is available (value 1)
}
pid_t pids[NUM_PHILOSOPHERS];
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pids[i] = fork();
if (pids[i] == 0) {
philosopher(i);
exit(0); // Child process should exit after finishing its work.
}
}
// Wait for all child processes to complete
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
wait(NULL);
}
// Destroy semaphores
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&forks[i]);
}
return 0;
}
Explanation:
-
Semaphores Initialization:
- An array of semaphores named
forks is used to control the access to each fork.
- Each semaphore is initialized to
1, indicating that each fork is available.
-
Philosopher Function:
- Each philosopher continuously thinks and then tries to pick up the forks (using
sem_wait).
- Once both forks are acquired, the philosopher eats and then puts down the forks (using
sem_post).
-
Main Function:
- The main function initializes the semaphores, creates the philosopher processes using
fork(), and waits for them to finish.
- After all philosopher processes have completed, the semaphores are destroyed using
sem_destroy().
-
Infinite Loop:
- The philosopher function runs an infinite loop simulating the thinking and eating process. In a real implementation, you might want to limit the number of times they eat.
Compilation and Execution:
To compile and run the program, follow these commands in your terminal:
gcc -o dining_philosophers dining_philosophers.c -lpthread
./dining_philosophers
Make sure to link the pthread library because semaphores are part of threading.
Output:
The output will show philosophers thinking and eating, illustrating their interactions with the forks and demonstrating the Dining Philosophers Problem. Each philosopher process operates independently but shares the semaphores to ensure proper resource access.