0% found this document useful (0 votes)
24 views2 pages

Ex - No. 7 - Dining Philosopher Problem

Uploaded by

SDHN Family
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)
24 views2 pages

Ex - No. 7 - Dining Philosopher Problem

Uploaded by

SDHN Family
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/ 2

Ex. No.

7 Dining Philosopher problem Date :

AIM:

To write C programs to simulate solutions to Dining Philosophers Problem.


DESCRIPTION:

The dining – philosophers problem is considered a classic synchronization problem because it


is an example of a large class of concurrency – control problems. It is a simple representation
of the need to allocate several resources among several processes in a deadlock-free and
starvation- free manner. Consider five philosophers who spend their lives thinking and eating.
The philosophers share a circular table surrounded by five chairs, each belonging to one
philosopher. In the center of the table is a bowl of rice, and the table is laid with five single
chopsticks. When a philosopher thinks, she does not interact with her colleagues. From time to
time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest to her
(the chopsticks that are between her and her left and right neighbours). A philosopher may pick
up only one chopstick at a time. Obviously, she cam1ot pick up a chopstick that is already in
the hand of a neighbour. When a hungry philosopher has both her chopsticks at the same time,
she eats without releasing her chopsticks. When she is finished eating, she puts down both of
her chopsticks and starts thinking again. The dining-philosophers problem may lead to a
deadlock situation and hence some rules have to be framed to avoid the occurrence of deadlock.

Program: [Complete the code]

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

sem_t room;
sem_t chopstick[5];
void * philosopher(void * num)
{
//Write coding for Main Philosopher thread

void eat(int phil)


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

int main()
{
int i,a[5]; pthread_t tid[5];

sem_init(&room,0,4);

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

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

Output:

Verified by
Faculty In-charge Sign : Date :

You might also like