-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBanker's Algorithm.c
74 lines (64 loc) · 1.89 KB
/
Banker's 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
#include <stdio.h>
int main(void)
{
int allocation[20][20], max[20][20], need[20][20], available[20], sequence[20];
int m, n, status[20], c = 0, completed = 0, count;
int i, j, step;
printf("Enter the number of processes and resources: ");
scanf("%d %d", &m, &n);
printf("Enter the allocated matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &allocation[i][j]);
}
}
printf("Enter the max matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &max[i][j]);
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
printf("Enter the available resources:\n");
for (i = 0; i < n; i++) {
scanf("%d", &available[i]);
}
for (i = 0; i < m; i++) {
status[i] = 0;
}
for (step = 0; step < m; step++) {
for (i = 0; i < m; i++) {
if (status[i] == 0) {
count = 0;
for (j = 0; j < n; j++) {
if (need[i][j] <= available[j]) {
count++;
}
}
if (count == n) {
for (j = 0; j < n; j++) {
available[j] += allocation[i][j];
}
status[i] = 1;
sequence[c] = i + 1;
c++;
completed++;
}
}
}
}
if (completed == m) {
printf("System is in a safe state.\nSafe sequence: ");
for (i = 0; i < m; i++) {
printf("%d ", sequence[i]);
}
printf("\n");
} else {
printf("System is in an unsafe state.\n");
}
return 0;
}