You are on page 1of 7

Course Name: Real time system Lab Course code: CSP-457

Experiment:1.4

Aim: Write a program to simulate the concept of Dining-Philosophers Problem


IDE USED: - Dev C++
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 neighbors). 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 neighbor. 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.

ALGORITHM: -

To solve this Dead Lock situation, Last philosopher (anyone can do this)

1. First try to take right side fork and then left side fork.

2. In our example 5th person tries to take 4th Fork Instead of 5th one

3. Since 4th Fork already taken by 4th the person, he gets nothing. But he left 5th Fork

4. Now the first person will take this 5th Fork and complete dinner and make 1st and 5th
available for remaining people

5. Next 2nd person takes 1st fork and completes and releases 1st and 2nd

6. This continuous until all finish’s dinner.

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457

Program code: -
#include<iostream>
#define n 4
using namespace std;
int compltedPhilo = 0,i;

struct fork{
int taken;
}ForkAvil[n];

struct philosp{
int left;
int right;
}Philostatus[n];

void goForDinner(int philID)


{
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
cout<<"Philosopher "<<philID+1<<" completed his dinner\n";

else if(Philostatus[philID].left==1 && Philostatus[philID].right==1)


{

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457
cout<<"Philosopher "<<philID+1<<" completed his dinner\n";

Philostatus[philID].left = Philostatus[philID].right = 10;


int otherFork = philID-1;

if(otherFork== -1)
otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0;
cout<<"Philosopher "<<philID+1<<" released fork "<<philID+1<<" and fork
"<<otherFork+1<<"\n";
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0)
{
if(philID==(n-1)){
if(ForkAvil[philID].taken==0)
{
ForkAvil[philID].taken = Philostatus[philID].right = 1;
cout<<"Fork "<<philID+1<<" taken by philosopher "<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID+1<<"\n";
}
}else{
int dupphilID = philID;

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457
philID-=1;

if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
cout<<"Fork "<<philID+1<<" taken by Philosopher "<<dupphilID+1<<"\n";
}else{
cout<<"Philosopher "<<dupphilID+1<<" is waiting for Fork
"<<philID+1<<"\n";
}
}
}
else if(Philostatus[philID].left==0)
{
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0)
{
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
cout<<"Fork "<<philID<<" taken by philosopher "<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID<<"\n";
}
}else{

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
cout<<"Fork "<<philID+1<<" taken by Philosopher "<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for Fork
"<<philID+1<<"\n";
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){

for(i=0;i<n;i++)
goForDinner(i);
cout<<"\nTill now num of philosophers completed dinner are "<<compltedPhilo<<"\n\n";
}
return 0;
}

NAME : Ahmad Massih Amir UID: 19BCS2850


Course Name: Real time system Lab Course code: CSP-457

Output:

Learning Outcomes:
i. Concept of dinning philosopher’s problem.

ii. Concept of deadlock and how to avoid such situations using this method.

iii. Uses and applications of this method

NAME : Ahmad Massih Amir UID: 19BCS2850

You might also like