-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deadlock Avoidance.c
206 lines (205 loc) · 3.89 KB
/
Deadlock Avoidance.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<stdio.h>
int count=0;
int safety_algo(int res,int n,int ava[],int need[][20],int all[][20])
{
int cal=0,finish[10],work[10],result[n],i,tt=0,color=0;
for(int i=0;i<res;i++)
{
work[i]=ava[i];
}
for(int i=0;i<n;i++)
{
finish[i]=0;
}
for(int k=0;k<n;k++)
{
for(int t=0;t<n;t++)
{
cal=0;
if(finish[t]==0)
{
for(int i=0;i<res;i++)
{
if(need[t][i]>work[i])
{
cal=1;
break;
}
}
if(cal==0)
{
for(int z=0;z<res;z++)
{
work[z]=work[z]+all[t][z];
}
result[tt]=t;
tt++;
finish[t]=1;
}
}
}
}
int host=0;
for(int i=0;i<n;i++)
{
if(finish[i]==0)
{
host=1;
printf("The following sequence is not safe\n");
break;
}
}
if(host==0)
{
printf("The safe sequence is:\n");
for(int i=0;i<n;i++)
{
printf("%d\t",result[i]);
color=1;
}
}
return color;
}
void res_request(int item,int res,int n,int req[],int need[][20],int ava[],int all[][20])
{
int flag=0,common=0,tat=0;
for(int t=0;t<res;t++)
{
if(req[t]>need[item][t])
{
printf("process %d has exceeded its maximum claim.",item);
flag=1;
return;
}
}
if(flag==0)
{
for(int i=0;i<res;i++)
{
if(req[i]>ava[i])
{
common=1;
printf("wait for resources \n");
}
}
if(common==0)
{
for(int p=0;p<res;p++)
{
ava[p]=ava[p]-req[p];
all[item][p]=all[item][p]+req[p];
need[item][p]=need[item][p]-req[p];
}
tat= safety_algo(res,n,ava,need,all);
if(tat==1)
{
printf("resources are allocated to process %d",item);
}
else
{
printf("process aborted \n");
for(int p=0;p<res;p++)
{
ava[p]=ava[p]+req[p];
all[item][p]=all[item][p]-req[p];
need[item][p]=need[item][p]+req[p];
}
printf("changes retransformed successfully\n");
}
}
}
}
int main()
{
int n,i,j,res,y;
printf("enter number of processes\n");
scanf("%d",&n);
printf("enter total number of resource types\n");
scanf("%d",&res);
int all[20][20],max[20][20],ava[20],need[20][20],a[20];
printf("enter number of instances of each resource\n");
for(i=0;i<res;i++)
{
printf("enter number of instances of resource %d: \n",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("enter resource allocation for process %d: \n",i+1);
for(j=0;j<res;j++)
{
printf("enter number of instances of resource %d for process %d\n",j+1,i+1);
scanf("%d",&all[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("enter maximum resource allocation for process %d:\n",i+1);
for(j=0;j<res;j++)
{
printf("enter maximum number of instances of type %d for process %d\n",j+1,i+1);
scanf("%d",&max[i][j]);
}
}
//printf("calculating total available resources\n");
for(j=0;j<res;j++)
{
int sum=0;
for(i=0;i<n;i++)
{
sum=sum+all[i][j];
}
//printf("%d\t",sum);
//printf("%d\t",a[j]);
ava[j]=a[j]-sum;
}
printf("\n");
/* for(i=0;i<res;i++)
{
printf("total available resources of instance %d are: \n",i+1);
printf("%d\n",ava[i]);
}*/
//printf("calculating need of resources \n");
for(i=0;i<n;i++)
{
for(j=0;j<res;j++)
{
need[i][j]=max[i][j]-all[i][j];
}
}
/*for(i=0;i<n;i++)
{
//printf("total resources required for process %d are:",i+1);
for(j=0;j<res;j++)
{
//printf("total rsources required for instance %d by process %d ",j+1,i+1);
printf("%d",need[i][j]);
}
printf("\n");
}*/
//implementing safety algorithm.
int var=0;
var=safety_algo(res,n,ava,need,all);
//implementing request algorithm
if(var==1)
{
do
{
int item,req[res];
printf("printf enter which process wants the request for additional sources : \n");
scanf("%d",&item);
for(i=0;i<res;i++)
{
printf("enter how many resources have been claimed for instance %d by process %d:c\n",i+1,item);
scanf("%d",&req[i]);
}
res_request(item,res,n,req,need,ava,all);
printf("Do u want to continue?? press 1 if you wish to continue\n");
scanf("%d",&y);
}while(y==1);
}
else
{
printf("since safe sequence is not printed requests for additional resources cant be processed\n");
}
}