-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBankers_Algorithm.c
70 lines (70 loc) · 1.72 KB
/
Bankers_Algorithm.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
#include<stdio.h>
int main(){
int i,j,k,n,r,alloc[10][10],max[10][10],avail[10],need[10][10],count,flag,order[10],fla[10];
printf("Enter no. of processes :- ");
scanf("%d",&n);
printf("Enter no. of resources :- ");
scanf("%d",&r);
printf("Enter Alocation Matrix :- \n");
for(i=0;i<n;i++){
for (j=0;j<r;j++){
printf("p[%d][%d] :- ",i,j);
scanf("%d",&alloc[i][j]);
}
fla[i]=0;
}
printf("Enter Maximum Matrix :- \n");
for(i=0;i<n;i++){
for (j=0;j<r;j++){
printf("p[%d][%d] :- ",i,j);
scanf("%d",&max[i][j]);
}
}
printf("Enter the Available Value :- \n");
for(i=0;i<r;i++){
scanf("%d",&avail[i]);
}
for(i=0;i<n;i++){
for(j=0;j<r;j++){
need[i][j]=max[i][j]-alloc[i][j];
}
}
count=0;
for(k=0;k<n;k++){
for(i=0;i<n;i++){
if(fla[i]==0){
flag=0;
for(j=0;j<r;j++){
if(need[i][j]>avail[j]){
flag=1;
}
}
if(flag==0){
order[count]=i;
count++;
fla[i]=1;
for(j=0;j<r;j++){
avail[j]=avail[j]+alloc[i][j];
}
}
}
}
}
flag=0;
for(i=0;i<n;i++){
if(fla[i]==0){
flag=1;
}
}
if(flag==0){
printf("It is in safe state\n");
printf("Safe sequence :- ");
for(i=0;i<count;i++){
printf("p[%d] ",order[i]);
}
}
else{
printf("It is not in safe sequence");
}
return 0;
}