-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbankers_algo.c
59 lines (59 loc) · 1.78 KB
/
bankers_algo.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
#include <stdio.h>
int main(){
int n, m, i, j, k, y, alloc[20][20], max[20][20], avail[50], ind = 0;
printf("Enter the no of Proceses:");
scanf("%d", &n);
printf("Enter the no of Resources:");
scanf("%d", &m);
printf("Enter the Allocation Matrix:");
for (i = 0; i < n; i++){
for (j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);
}
printf("Enter the Max Matrix:");
for (i = 0; i < n; i++){
for (j = 0; j < m; j++)
scanf("%d", &max[i][j]);
}
printf("Enter the Available Matrix");
for (i = 0; i < m; i++)
scanf("%d", &avail[i]); // Reading the available matrix
int finish[n], safesequence[n], work[m], need[n][m];
// calculating NEED matrix
for (i = 0; i < n; i++){
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
printf("NEED matrix is");
for (i = 0; i < n; i++){
printf("\n");
for (j = 0; j < m; j++)
printf(" %d ", need[i][j]);
}
for (i = 0; i < m; i++)
work[i] = avail[i];
for (i = 0; i < n; i++)
finish[i] = 0;
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;
break;
}
}
if (flag == 0){
safesequence[ind++] = i;
for (y = 0; y < m; y++)
work[y] += alloc[i][y];
finish[i] = 1;
}
}
}
}
printf("\nFollowing is the SAFE Sequence\n");
for (i = 0; i <= n - 1; i++)
printf(" P%d ", safesequence[i]);
}