-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanker.cpp
More file actions
186 lines (158 loc) · 3.99 KB
/
Copy pathbanker.cpp
File metadata and controls
186 lines (158 loc) · 3.99 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string filename = "input.txt";
ifstream in(filename);
if (!in)
{
cerr << "Error: could not open file " << filename << endl;
return 1;
}
int nProc, nRes;
in >> nProc >> nRes;
vector<vector<int>> allocation(nProc, vector<int>(nRes));
vector<vector<int>> maxm(nProc, vector<int>(nRes));
vector<int> available(nRes);
// Read Allocation matrix
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
in >> allocation[i][j];
}
}
// Read Max matrix
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
in >> maxm[i][j];
}
}
// Read Available vector
for (int j = 0; j < nRes; ++j)
{
in >> available[j];
}
in.close();
// Compute Need matrix: Need = Max - Allocation
vector<vector<int>> need(nProc, vector<int>(nRes));
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
need[i][j] = maxm[i][j] - allocation[i][j];
}
}
cout << "==============================\n";
cout << " Banker's Algorithm Check \n";
cout << "==============================\n\n";
cout << "Number of processes: " << nProc << endl;
cout << "Number of resource types: " << nRes << endl;
cout << "\nAllocation matrix:\n";
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
cout << "\nMax matrix:\n";
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
cout << maxm[i][j] << " ";
}
cout << endl;
}
cout << "\nAvailable vector:\n";
for (int j = 0; j < nRes; ++j)
{
cout << available[j] << " ";
}
cout << endl;
cout << "\nNeed matrix (Max - Allocation):\n";
for (int i = 0; i < nProc; ++i)
{
for (int j = 0; j < nRes; ++j)
{
cout << need[i][j] << " ";
}
cout << endl;
}
// Banker's safety algorithm
vector<int> work = available;
vector<bool> finish(nProc, false);
vector<int> safeSequence;
int finishedCount = 0;
while (finishedCount < nProc)
{
bool foundProcess = false;
for (int i = 0; i < nProc; ++i)
{
if (!finish[i])
{
bool canFinish = true;
for (int j = 0; j < nRes; ++j)
{
if (need[i][j] > work[j])
{
canFinish = false;
break;
}
}
if (canFinish)
{
for (int j = 0; j < nRes; ++j)
{
work[j] += allocation[i][j];
}
finish[i] = true;
safeSequence.push_back(i);
finishedCount++;
foundProcess = true;
break;
}
}
}
if (!foundProcess)
{
break;
}
}
bool allFinished = true;
for (int i = 0; i < nProc; ++i)
{
if (!finish[i])
{
allFinished = false;
break;
}
}
cout << "\n------------------------------\n";
if (allFinished)
{
cout << "System is in a SAFE state.\n";
cout << "Safe sequence: ";
for (int k = 0; k < (int)safeSequence.size(); ++k)
{
cout << "P" << safeSequence[k];
if (k != (int)safeSequence.size() - 1)
{
cout << " -> ";
}
}
cout << endl;
}
else
{
cout << "System is NOT in a safe state.\n";
}
cout << "------------------------------\n";
return 0;
}