-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinning_new.c
95 lines (84 loc) · 1.35 KB
/
dinning_new.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
#define size 5
int eat(int tb[],int cs[],int p)
{
if(cs[p]==1 && cs[(p+1)%size]==1)
{
printf("philosopher %d is eating\n",p+1);
cs[p]=0;
cs[(p+1)%size]=0;
tb[p]=1;
return 0;
}
else
return -1;
}
void think(int tb[],int cs[],int p)
{
if(cs[p]!=0 || cs[(p+1)%size]!=0)
{
return;
}
else
{
cs[p]=1;
cs[(p+1)%size]=1;
tb[p]=0;
printf("chopstick %d anad %d are placed\n",p+1,(p+2)%size);
printf("philosopher %d is thinkning\n",p+1);
}
}
int main()
{
int tb[5]={0};
int cs[5]={1,1,1,1,1};
int hq[10]={0};
int sq[10]={0};
int j=-1,j1=-1;
for(int i=0;i<5;i++)
{
int x=eat(tb,cs,i);
if(x==-1)
{
hq[++j]=i+1;
}
else
{
sq[++j1]=i+1;
}
}
printf("Hungry queue:\n");
for(int i=0;i<=j;i++)
{
printf("%d ",hq[i]);
}
printf("\n");
while(j1>-1)
{
think(tb,cs,sq[j1]-1);
j1--;
}
int count=0;
while(count<=j)
{
for(int i=0;i<=j;i++)
{
if(hq[i]!=0)
{
int x=eat(tb,cs,hq[i]-1);
if(x==0)
{
sq[++j1]=hq[i];
hq[i]=0;
count++;
}
}
}
for(int i=0;i<=j1;i++)
{
think(tb,cs,sq[i]-1);
}
}
return 0;
}