forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankers Algorithm
More file actions
98 lines (83 loc) · 2.37 KB
/
Bankers Algorithm
File metadata and controls
98 lines (83 loc) · 2.37 KB
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
#include <stdio.h>
#include <stdbool.h>
#define P 5 // Number of processes
#define R 3 // Number of resources
// Function to find if the system is in a safe state or not
bool isSafe(int processes[], int avail[], int max[][R], int allot[][R]) {
int need[P][R];
bool finish[P] = {false};
int safeSeq[P];
int work[R];
int count = 0;
// Calculate the 'need' matrix
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
// Initialize work[] as available[]
for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
// Find a process that can be executed
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) {
int j;
for (j = 0; j < R; j++) {
if (need[p][j] > work[j]) {
break;
}
}
// If all 'need' values are less than available, we can allocate resources
if (j == R) {
for (int k = 0; k < R; k++) {
work[k] += allot[p][k];
}
safeSeq[count++] = p;
finish[p] = true;
found = true;
break;
}
}
}
// If no process could be executed, the system is not in a safe state
if (!found) {
printf("System is not in a safe state.\n");
return false;
}
}
// If the loop ends, the system is in a safe state
printf("System is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < P; i++) {
printf("%d ", safeSeq[i]);
}
printf("\n");
return true;
}
int main() {
// Available resources
int avail[] = {3, 3, 2};
// Maximum resources required by each process
int max[][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
// Resources currently allocated to each process
int allot[][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
// Process IDs
int processes[] = {0, 1, 2, 3, 4};
// Check if the system is in a safe state
isSafe(processes, avail, max, allot);
return 0;
}