OPERATING SYSTM’s ALGORITHM (In LINUX OS) - DINING PHILOSOPHERS PROBLEM


DINING PHILOSOPHERS PROBLEM



PROGRAM:

#include<stdio.h>
int i,j,pn,ch,k,l;
char state[5];
int thinking=0;
int hungry=1;
int eating=2;
main()
{
do
{
printf(“\nEnter the choice\n”);
printf(“1.Pick up\n2.Put down\n3.Exit\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“Enter the philosopher number”);
scanf(“%d”,&pn);
pickup(pn);
break;
case 2:
printf(“Enter the philosopher number”);
scanf(“%d”,&pn);
putdown(pn);
break;
}
} while(ch!=3);
}


int pickup(int i)
{
state[i]=hungry;
test(i);
if(state[i]!=eating)
printf(“Philosopher %d has to wait\n’,i);
}
int putdown(int k)
{
state[k]=thinking;
test((k+4)%5);
test((k+1)%5);
}
int test(int l)
{
if(state[(l+4)%5]!=eating&&state[l]==hungry&&state[(l+1)%5!=eating)
{
state[l]=eating;
printf(“Philosopher  %d can eat\n”,l);
}
}


OUTPUT:

Enter the choice
1.Pick up
2.Put down
3.Exit
1
Enter the philosopher number 1
Philosopher 1can eat
Enter the choice
1.Pick up
2.Put down
3.Exit
1
Enter the philosopher number 3
Philosopher 3 has to wait
Enter the choice
1.Pick up
2.Put down
3.Exit
2
Enter the philosopher number 2
Philosopher 3 can eat
Enter the choice
1.Pick up
2.Put down
3.Exit

Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

OPERATING SYSTM’s ALGORITHM (In LINUX OS) - FIRST FIT, BEST FIT, WORST FIT ALGORITHM