-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankers_Algm.c
66 lines (66 loc) · 1.25 KB
/
Bankers_Algm.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
#include <stdio.h>
void main()
{
int n,m,i,j,k;
printf("Enter the number of processes : ");
scanf("%d",&n);
printf("Enter the number of resources : ");
scanf("%d",&m);
int allocation[n][m], maximum[n][m], available[m];
printf("Enter the allocation matrix for each process :\n");
for(i=0;i<n;i++) {
printf("P%d : ",i);
for(j=0;j<m;j++) {
scanf("%d",&allocation[i][j]);
}
}
printf("Enter the maximum matrix for each process :\n");
for(i=0;i<n;i++) {
printf("P%d : ",i);
for(j=0;j<m;j++) {
scanf("%d",&maximum[i][j]);
}
}
printf("Enter the available resources : ");
for(k=0;k<m;k++)
scanf("%d",&available[k]);
int finish[n],ans[n],ind=0,need[n][m];
for(k=0;k<n;k++)
finish[k]=0;
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
need[i][j] = maximum[i][j]-allocation[i][j];
}
}
int y=0,flag;
for(k=0;k<n;k++)
for(i=0;i<n;i++)
if(finish[i]==0) {
flag=0;
for(j=0;j<m;j++)
if(need[i][j]>available[j]) {
flag=1;
break;
}
if(flag==0) {
ans[ind++]=i;
for(y=0;y<m;y++)
available[y] += allocation[i][y];
finish[i]=1;
}
}
flag = 1;
for(i=0;i<n;i++) {
if(finish[i]==0) {
flag=0;
printf("\nThe following system is not safe\n");
break;
}
}
if(flag==1) {
printf("\nFollowing is system is safe and the SAFE Sequence is :\n");
for(i=0;i<n-1;i++)
printf("P%d -> ",ans[i]);
printf(" P%d\n",ans[n-1]);
}
}