You are on page 1of 5

#include <stdio.

h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define NUM_PHILOSOPHERS 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2

sem_t mutex;
sem_t semaphores[NUM_PHILOSOPHERS];
int state[NUM_PHILOSOPHERS];

void test(int philosopher_id) {


if (state[philosopher_id] == HUNGRY &&
state[(philosopher_id + 1) % NUM_PHILOSOPHERS] != EATING &&
state[(philosopher_id + NUM_PHILOSOPHERS - 1) % NUM_PHILOSOPHERS] !=
EATING) {
state[philosopher_id] = EATING;
printf("Philosopher %d is eating.\n", philosopher_id);
sem_post(&semaphores[philosopher_id]);
}
}

void grab_forks(int philosopher_id) {


sem_wait(&mutex);
state[philosopher_id] = HUNGRY;
printf("Philosopher %d is hungry.\n", philosopher_id);
test(philosopher_id);
sem_post(&mutex);
sem_wait(&semaphores[philosopher_id]);
}

void put_forks(int philosopher_id) {


sem_wait(&mutex);
state[philosopher_id] = THINKING;
printf("Philosopher %d is done eating and thinking.\n", philosopher_id);
test((philosopher_id + 1) % NUM_PHILOSOPHERS); // Test left neighbor
test((philosopher_id + NUM_PHILOSOPHERS - 1) % NUM_PHILOSOPHERS); // Test
right neighbor
sem_post(&mutex);
}
void *philosopher(void *arg) {
int philosopher_id = *((int *)arg);

while (1) {
// Philosopher is thinking
printf("Philosopher %d is thinking.\n", philosopher_id);
sleep(1); // Simulate thinking time

// Philosopher is hungry and wants to eat


grab_forks(philosopher_id);

// Philosopher is eating
printf("Philosopher %d is eating.\n", philosopher_id);
sleep(1); // Simulate eating time

// Philosopher is done eating, release forks


put_forks(philosopher_id);
}

pthread_exit(NULL);
}
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
sem_init(&mutex, 0, 1);
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&semaphores[i], 0, 0);
philosopher_ids[i] = i;
pthread_create(&philosophers[i], NULL, philosopher, &philosopher_ids[i]);
}
// Let the simulation run for a while
sleep(10);
// Terminate the philosophers
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_cancel(philosophers[i]);
}
// Join the philosophers
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(philosophers[i], NULL);
}
sem_destroy(&mutex);
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&semaphores[i]);
}
return 0;
}
OUTPUT:

it21100@slave4:~/os/lab9$ ./dps
Philosopher 0 is thinking.
Philosopher 1 is thinking.
Philosopher 2 is thinking.
Philosopher 3 is thinking.
Philosopher 4 is thinking.
Philosopher 0 is hungry.
Philosopher 0 is eating.
Philosopher 0 is eating.
Philosopher 1 is hungry.
Philosopher 2 is hungry.
Philosopher 2 is eating.
Philosopher 2 is eating.
Philosopher 3 is hungry.
Philosopher 4 is hungry.
Philosopher 0 is thinking.
Philosopher 4 is eating.
Philosopher 0 is thinking.
Philosopher 4 is eating.
Philosopher 2 is thinking.
Philosopher 1 is eating.
Philosopher 2 is thinking.
Philosopher 1 is eating.
Philosopher 0 is hungry.
Philosopher 4 is thinking.
Philosopher 3 is eating.
Philosopher 4 is thinking.
Philosopher 2 is hungry.
Philosopher 3 is eating.
Philosopher 1 is thinking.
Philosopher 0 is eating.
Philosopher 1 is thinking.
Philosopher 0 is eating.
Philosopher 4 is hungry.
Philosopher 3 is thinking.
Philosopher 2 is eating.
Philosopher 3 is thinking.
Philosopher 1 is hungry.
Philosopher 2 is eating.
Philosopher 0 is thinking.
Philosopher 4 is eating.
Philosopher 0 is thinking.
Philosopher 4 is eating.
Philosopher 3 is hungry.
Philosopher 2 is thinking.
Philosopher 1 is eating.
Philosopher 2 is thinking.
Philosopher 0 is hungry.
Philosopher 4 is thinking.
Philosopher 3 is eating.
Philosopher 4 is thinking.
Philosopher 1 is eating.
Philosopher 3 is eating.
Philosopher 2 is hungry.
Philosopher 4 is hungry.
Philosopher 3 is thinking.
Philosopher 4 is eating.
Philosopher 3 is thinking.
Philosopher 1 is thinking.
Philosopher 2 is eating.
Philosopher 1 is thinking.
Philosopher 4 is eating.
Philosopher 2 is eating.
Philosopher 1 is hungry.
Philosopher 4 is thinking.
Philosopher 0 is eating.
Philosopher 4 is thinking.
Philosopher 0 is eating.
Philosopher 3 is hungry.
Philosopher 2 is thinking.
Philosopher 3 is eating.
Philosopher 2 is thinking.
Philosopher 3 is eating.
Philosopher 4 is hungry.
Philosopher 0 is thinking.
Philosopher 1 is eating.
Philosopher 0 is thinking.
Philosopher 1 is eating.
Philosopher 2 is hungry.
Philosopher 3 is thinking.
Philosopher 4 is eating.
Philosopher 3 is thinking.
Philosopher 4 is eating.
Philosopher 0 is hungry.
Philosopher 1 is thinking.
Philosopher 2 is eating.
Philosopher 1 is thinking.
Philosopher 2 is eating.
Philosopher 3 is hungry.
Philosopher 4 is thinking.
Philosopher 0 is eating.
Philosopher 4 is thinking.
Philosopher 0 is eating.
it21100@slave4:~/os/lab9$

You might also like