-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankerAlg.cpp
More file actions
76 lines (68 loc) · 2.07 KB
/
BankerAlg.cpp
File metadata and controls
76 lines (68 loc) · 2.07 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
#include "bankerAlg.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
BankersAlgorithm::BankersAlgorithm(const std::string& filePath) {
readDataFromFile(filePath);
}
void BankersAlgorithm::readDataFromFile(const std::string& filePath) {
std::ifstream file(filePath);
std::string line;
if (!file) {
std::cerr << "Cannot open input file!" << std::endl;
exit(1);
}
// Read Available Resources
std::getline(file, line); // Skip the comment line
std::getline(file, line);
std::istringstream iss(line);
int res;
while (iss >> res) {
available.push_back(res);
}
// Skip to Allocation Matrix
std::getline(file, line); // Skip the comment line
for (int i = 0; i < numProcesses; i++) {
std::getline(file, line);
std::istringstream iss(line);
std::vector<int> row;
while (iss >> res) {
row.push_back(res);
}
allocation.push_back(row);
}
// Skip to Maximum Matrix
std::getline(file, line); // Skip the comment line
for (int i = 0; i < numProcesses; i++) {
std::getline(file, line);
std::istringstream iss(line);
std::vector<int> row;
while (iss >> res) {
row.push_back(res);
}
maximum.push_back(row);
}
}
void BankersAlgorithm::calculateSafeSequence() {
if (isSafe()) {
std::vector<int> sequence = findSafeSequence();
std::cout << "System is in a safe state.\nSafe sequence is: ";
for (int i : sequence) {
std::cout << "P" << i;
if(i < sequence.size() -1){
std::cout << "-->";
}
}
std::cout << std::endl;
} else {
std::cout << "No safe sequence found. System is not in a safe state." << std::endl;
}
}
bool BankersAlgorithm::isSafe() {
// Implementation of isSafe logic
return true; // Placeholder
}
std::vector<int> BankersAlgorithm::findSafeSequence() {
// Implementation to find safe sequence
return std::vector<int>{0, 1, 2, 3, 4}; // Placeholder
}