-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanker.cpp
More file actions
136 lines (115 loc) · 4.03 KB
/
Copy pathbanker.cpp
File metadata and controls
136 lines (115 loc) · 4.03 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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int NUM_PROCESSES = 5;
const int NUM_RESOURCES = 3;
// Function to read data from input file
// And store it in the relevant data structures
void readInput(vector<int>& available, vector<vector<int>>& allocation,
vector<vector<int>>& max) {
ifstream file("input.txt");
// Read Available table
available.resize(NUM_RESOURCES);
for (int i = 0; i < NUM_RESOURCES; i++) {
file >> available[i];
}
// Skip blank line
string line;
getline(file, line); getline(file, line);
// Read Allocation table
allocation.resize(NUM_PROCESSES, vector<int>(NUM_RESOURCES));
for (int i = 0; i < NUM_PROCESSES; i++) {
for (int j = 0; j < NUM_RESOURCES; j++) {
file >> allocation[i][j];
}
}
// Skip blank line
getline(file, line); getline(file, line);
// Read Max table
max.resize(NUM_PROCESSES, vector<int>(NUM_RESOURCES));
for (int i = 0; i < NUM_PROCESSES; i++) {
for (int j = 0; j < NUM_RESOURCES; j++) {
file >> max[i][j];
}
}
}
// Apply banker's algorithm on given data
// If system in safe state, return true and store safe sequence
// Otherwise, return false
bool bankersAlgorithm(const vector<int>& available,
const vector<vector<int>>& allocation,
const vector<vector<int>>& need,
vector<int>& safeSequence) {
vector<int> work = available;
vector<bool> finish(NUM_PROCESSES, false);
safeSequence.clear();
bool found;
int finished = 0;
// Try to find a safe sequence
while (finished < NUM_PROCESSES) {
found = false;
for (int i = 0; i < NUM_PROCESSES; i++) {
// The process can be satisfied,
// unless it needs more instances of a resource
// than what is available
bool canSatisfy = true;
for (int j = 0; j < NUM_RESOURCES; j++) {
if (need[i][j] > work[j]) {
canSatisfy = false;
}
}
// If process i not finished and can be satisfied
if (!finish[i] && canSatisfy) {
// "Finish" process i
// Its resources are added to Work
for (int j = 0; j < NUM_RESOURCES; j++) {
work[j] += allocation[i][j];
}
finish[i] = true;
// Place process i in the safe sequence
safeSequence.push_back(i);
found = true;
finished++;
break;
}
}
// If no unfinished process can be satisfied,
// System is in unsafe state
if (!found) {
return false;
}
}
return true;
}
int main() {
vector<int> available;
vector<vector<int>> allocation;
vector<vector<int>> max;
vector<vector<int>> need;
vector<int> safeSequence;
// Read input from file
readInput(available, allocation, max);
// Calculate Need matrix
need.resize(NUM_PROCESSES, vector<int>(NUM_RESOURCES));
for (int i = 0; i < NUM_PROCESSES; i++) {
for (int j = 0; j < NUM_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
// Run Banker's Algorithm
bool isSafe = bankersAlgorithm(available, allocation, need, safeSequence);
// Output results
if (isSafe) {
cout << "The system is in a safe state." << endl;
cout << "Safe sequence: ";
for (size_t i = 0; i < safeSequence.size(); i++) {
cout << "P" << safeSequence[i];
if (i < safeSequence.size() - 1) cout << " -> ";
}
cout << endl;
} else {
cout << "The system is not in a safe state." << endl;
}
}