-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeadlock avoidance.c
71 lines (70 loc) · 1.58 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
#include <stdio.h>
#include <conio.h>
void main(){
int r[1][10],av[1][10];
int all[10][10],max[10][10],ne[10][10],w[10],safe[10];
int i=0,j=0,k=0,l=0,np=0,nr=0,count=0,cnt=0;
printf("enter the number of processes in a system: ");
scanf("%d",&np);
printf("enter the number of resources in a system: ");
scanf("%d",&nr);
for(i=1;i<=nr;i++){
printf("\n enter the number of instances of resource R%d " ,i);
scanf("%d",&r[0][i]);
av[0][i]=r[0][i]; }
for(i=1;i<=np;i++)
for(j=1;j<=nr;j++)
all[i][j]=ne[i][j]=max[i][j]=w[i]=0;
printf("\nEnter the allocation matrix");
for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++) {
scanf("%d",&all[i][j]);
av[0][j]=av[0][j]
-all[i][j];
}}
printf("\nEnter the maximum matrix");
for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++) {
scanf("%d",&max[i][j]); }}
for(i=1;i<=np;i++) {
for(j=1;j<=nr;j++) {
ne[i][j]=max[i][j]
-all[i][j];
}}
for(i=1;i<=np;i++) {
printf("pocess P%d",i);
for(j=1;j<=nr;j++) {
printf("\n allocated %d\t",all[i][j]);
printf("maximum %d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n_________________________\n");
}
printf("\nAvailability");
for(i=1;i<=nr;i++)
printf("R%d %d\t",i,av[0][i]);
printf("\n____________");
printf("\n safe sequence");
for(count=1;count<=np;count++) {
for(i=1;i<=np;i++)
{ cnt=0;
for(j=1;j<=nr;j++) {
if(ne[i][j]<=av[0][j] && w[i]==0)
cnt++; }
if(cnt==nr) {
k++;
safe[k]=i;
for(l=1;l<=nr;l++)
av[0][l]=av[0][l]+all[i][l];
printf("\n P%d ",safe[k]);
printf("\t Availability");
for(l=1;l<=nr;l++)
printf("R%d %d\t",l,av[0][l]);
w[i]=1;
}
}
}
getch();
}