0% found this document useful (0 votes)
14 views6 pages

Experiment 9

Uploaded by

ajayjain3901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Experiment 9

Uploaded by

ajayjain3901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 9

Sample 1

Aim

To implement a solution for the Dining Philosophers problem using synchronization


primitives such as semaphores or mutexes.

Algorithm

The basic idea is to prevent deadlock and ensure that no philosopher is starving (waiting
indefinitely). The solution uses semaphores to ensure mutual exclusion while picking up
chopsticks.

Pseudo Code

Here's the pseudo code for the Dining Philosophers problem using semaphores:

1. Initialize Semaphores:
○ Initialize one semaphore for each chopstick.
○ Initialize a semaphore to limit the number of philosophers that can sit at the
table simultaneously to N-1.
2. Philosopher's Routine:
○ Wait for the semaphore that limits the number of philosophers.
○ Wait for the semaphore for the left chopstick.
○ Wait for the semaphore for the right chopstick.
○ Eat.
○ Signal the semaphore for the right chopstick.
○ Signal the semaphore for the left chopstick.
○ Signal the semaphore that limits the number of philosophers.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define NUM_PHILOSOPHERS 5
#define NUM_CHOPSTICKS 5

sem_t room; // counting semaphore to limit the number of philosophers


sem_t chopstick[NUM_CHOPSTICKS]; // binary semaphore for each chopstick
void *philosopher(void *num);
void eat(int phil);

int main() {
int i, a[NUM_PHILOSOPHERS];
pthread_t tid[NUM_PHILOSOPHERS]; // threads for philosophers

// Initialize the room semaphore to 4 (allow up to 4 philosophers to sit)


sem_init(&room, 0, NUM_PHILOSOPHERS - 1);

// Initialize the chopstick semaphores


for(i = 0; i < NUM_CHOPSTICKS; i++) {
sem_init(&chopstick[i], 0, 1);
}

// Create philosopher threads


for(i = 0; i < NUM_PHILOSOPHERS; i++) {
a[i] = i;
pthread_create(&tid[i], NULL, philosopher, (void *)&a[i]);
}

// Wait for all philosopher threads to finish


for(i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(tid[i], NULL);
}

return 0;
}

void *philosopher(void *num) {


int phil = *(int *)num;

// Limit the number of philosophers


sem_wait(&room);

printf("\nPhilosopher %d has entered room", phil);

// Pick up left chopstick


sem_wait(&chopstick[phil]);
// Pick up right chopstick
sem_wait(&chopstick[(phil + 1) % NUM_CHOPSTICKS]);

// Eat
eat(phil);
sleep(2);
printf("\nPhilosopher %d has finished eating", phil);

// Put down right chopstick


sem_post(&chopstick[(phil + 1) % NUM_CHOPSTICKS]);
// Put down left chopstick
sem_post(&chopstick[phil]);

// Leave room
sem_post(&room);

return NULL;
}

void eat(int phil) {


printf("\nPhilosopher %d is eating", phil);
}

Output-
Sample -2

Aim

The aim of the second sample program is to implement the Dining Philosophers problem
using mutexes to synchronize access to shared resources (chopsticks). This implementation
ensures that no two philosophers can pick up the same chopstick simultaneously, preventing
deadlock and starvation.

Pseudo Code

Here is the pseudo code for the Dining Philosophers problem using mutexes:

1. Initialize Mutexes:
○ Initialize one mutex for each chopstick.
2. Philosopher's Routine:
○ Think.
○ Lock the left chopstick's mutex.
○ Lock the right chopstick's mutex.
○ Eat.
○ Unlock the right chopstick's mutex.
○ Unlock the left chopstick's mutex.
○ Finish eating and go back to thinking.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define NUM_PHILOSOPHERS 5
#define NUM_CHOPSTICKS 5

pthread_t philosopher[NUM_PHILOSOPHERS];
pthread_mutex_t chopstick[NUM_CHOPSTICKS];

void* dine(void* num);

int main() {
int i, status_message;
void* msg;

// Initialize the chopstick mutexes


for (i = 0; i < NUM_CHOPSTICKS; i++) {
status_message = pthread_mutex_init(&chopstick[i], NULL);
if (status_message != 0) {
printf("\n Mutex initialization failed");
exit(1);
}
}

// Create philosopher threads


for (i = 0; i < NUM_PHILOSOPHERS; i++) {
status_message = pthread_create(&philosopher[i], NULL, dine,
(void*)(size_t)i);
if (status_message != 0) {
printf("\n Thread creation error \n");
exit(1);
}
}

// Wait for all philosopher threads to finish


for (i = 0; i < NUM_PHILOSOPHERS; i++) {
status_message = pthread_join(philosopher[i], &msg);
if (status_message != 0) {
printf("\n Thread join failed \n");
exit(1);
}
}

// Destroy the chopstick mutexes


for (i = 0; i < NUM_CHOPSTICKS; i++) {
status_message = pthread_mutex_destroy(&chopstick[i]);
if (status_message != 0) {
printf("\n Mutex destroy failed \n");
exit(1);
}
}
return 0;
}

void* dine(void* num) {


int n = (int)(size_t)num;
printf("\nPhilosopher %d is thinking", n);

// Pick up chopsticks
if (n == NUM_PHILOSOPHERS - 1) {
pthread_mutex_lock(&chopstick[(n + 1) % NUM_CHOPSTICKS]);
pthread_mutex_lock(&chopstick[n]);
} else {
pthread_mutex_lock(&chopstick[n]);
pthread_mutex_lock(&chopstick[(n + 1) % NUM_CHOPSTICKS]);
}

// Eat
printf("\nPhilosopher %d is eating", n);
sleep(3);

// Put down chopsticks


pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n + 1) % NUM_CHOPSTICKS]);

printf("\nPhilosopher %d finished eating", n);


return NULL;
}

Output -

You might also like