-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bankers Algorithm.c
137 lines (115 loc) · 3.01 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int need[100][100],allot[100][100],max[100][100],available[100];
bool isFinished[100];
int sequence[100];
void isSafe(int N,int M)
{
int i,j,work[100],count=0;
for(i=0;i<M;i++)
work[i]=available[i];
for(i=0;i<100;i++)
isFinished[i]=false;
while(count<N)
{
bool canAllot=false;
for(i=0;i<N;i++)
{
if(isFinished[i]==false)
{
for(j=0;j<M;j++)
{
if(work[j]<need[i][j])
{
break;
}
}
if(j==M)
{
for(j=0;j<M;j++)
{
work[j]+=allot[i][j];
}
sequence[count++]=i;
isFinished[i]=true;
canAllot=true;
}
}
}
if(canAllot==false)
{
printf("System Is not safe\n");
return ;
}
}
printf("System is in safe state\n");
printf("Safe sequence :");
for(i=0;i<N;i++)
printf("%d ",sequence[i]);
printf("\n");
}
int main()
{
int i,j,N,M;
printf("Enter the number of process and resources :");
scanf("%d %d",&N,&M);
printf("Enter the available resources :\n");
for(i=0;i<M;i++)
scanf("%d",&available[i]);
printf("Enter the Allocation Matrix :\n");
for(i=0;i<N;i++)
for(j=0;j<M;j++)
scanf("%d",&allot[i][j]);
printf("Enter the matrix for maximum demand of each process :\n");
for(i=0;i<N;i++)
for(j=0;j<M;j++)
scanf("%d",&max[i][j]);
//calculation of need matrix
for(i=0;i<N;i++)
for(j=0;j<M;j++)
need[i][j]=max[i][j]-allot[i][j];
isSafe(N,M);
int indx,arr[100];
printf("Enter the process no for resource request :");
scanf("%d",&indx);
printf("Enter the requested instances of Each :");
for(i=0;i<M;i++)
scanf("%d",&arr[i]);
for(i=0;i<M;i++)
{
if( need[indx][i]<arr[i])
{
printf("Cannot request\n");
break;
}
}
if(i==M)
{
for(i=0;i<M;i++)
{
allot[indx][i]+=arr[i];
available[i]-=arr[i];
need[indx][i]-=arr[i];
}
isSafe(N,M);
}
}
/*nj
Allocation
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max allocation
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Avialiable
3 3 2
Request Of process 1
1 0 2
*/