-
Notifications
You must be signed in to change notification settings - Fork 5
/
BankersAlgorithm.c
68 lines (68 loc) · 1.67 KB
/
BankersAlgorithm.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
#include<stdio.h>
int main(){
int n,m;
printf("Enter no. of Processes:");
scanf("%d",&n);
printf("Enter no. of resources:");
scanf("%d",&m);
int Allocation[n][m],Max[n][m],Available[m],Need[n][m],Work[m],Finish[n],safesequence[n],ind=0,completed=0;
printf("Enter Allocation matrix:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&Allocation[i][j]);
}
}
printf("Enter Maximum matrix:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&Max[i][j]);
}
}
printf("Enter Available Resources:");
for(int i=0;i<m;i++){
scanf("%d",&Available[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
Need[i][j]=Max[i][j]-Allocation[i][j];
}
}
for(int i=0;i<n;i++){
Finish[i]=0;
}
for(int i=0;i<m;i++){
Work[i]=Available[i];
}
while(completed!=n){
for(int i=0;i<n;i++){
if(Finish[i]==0){
int flag=0;
for(int j=0;j<m;j++){
if(Need[i][j]>Work[j]){
flag=1;
break;
}
}
if(flag==0){
safesequence[ind++]=i;
for(int k=0;k<m;k++){
Work[k]+=Allocation[i][k];
}
Finish[i]=1;
completed++;
}
}
}
}
printf("Need Matrix:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d ",Need[i][j]);
}
printf("\n");
}
printf("Safe Sequence:");
for(int i=0;i<n;i++){
printf("%d\t",safesequence[i]);
}
}