-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbankers.c
More file actions
124 lines (93 loc) · 1.7 KB
/
Copy pathbankers.c
File metadata and controls
124 lines (93 loc) · 1.7 KB
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
#include <stdio.h>
int main()
{
int n,m,i,j,k,y,alloc[20][20],max[20][20],avail[50],ind=0,h;
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the number of resources\n");
scanf("%d",&m);
printf("Enter the allocated matrix\n");
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter max matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter available matrix\n");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
int finish[n], safesequence[n],work[m],need[n][m];
for(i=0;i<m;i++)
{
work[i]=avail[i];
}
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("Need Matrix\n");
for(i=0;i<n;i++)
{ printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",need[i][j]);
}
}
printf("\n");
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>work[j])
{
flag=1;
h=1;
break;
}
}
if(flag==0)
{
safesequence[ind++]=i;
for(y=0;y<m;y++)
work[y]+=alloc[i][y];
finish[i]=1;
h=0;
}
}
}
}
if(h==0)
{
printf("The system is in safe state\n");
printf("Safe sequence : \n");
for(i=0;i<n;i++)
printf("p%d",safesequence[i]);
}
else
{
printf("The system is unsafe");
}
printf("\n");
}