Professional Documents
Culture Documents
Dps
Dps
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];
while (1) {
// Philosopher is thinking
printf("Philosopher %d is thinking.\n", philosopher_id);
sleep(1); // Simulate thinking time
// Philosopher is eating
printf("Philosopher %d is eating.\n", philosopher_id);
sleep(1); // Simulate eating time
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$