-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (132 loc) · 3.85 KB
/
main.cpp
File metadata and controls
156 lines (132 loc) · 3.85 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
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
// Constants
const int PROCESS_COUNT = 5;
const int RESOURCE_COUNT = 3;
const int ATTRIBUTE_COUNT = 2;
// Globals
std::vector<int> safeOrder = {};
// Prototypes
bool bankers(int[], int[][RESOURCE_COUNT], int[][RESOURCE_COUNT]);
using std::string;
int main()
{
// Resource info
int available[RESOURCE_COUNT];
int instances[RESOURCE_COUNT];
// Allocated here to avoid a copy later
int work[RESOURCE_COUNT];
// Process info
int maximumResources[PROCESS_COUNT][RESOURCE_COUNT];
int allocatedResource[PROCESS_COUNT][RESOURCE_COUNT];
// Open file to read
std::ifstream input("input.txt");
// Reusable str buffer
string buffer;
// Load available resource counts
for (int i = 0; i < RESOURCE_COUNT; i++)
{
input >> buffer;
int val = std::stoi(buffer);
available[i] = val;
work[i] = val; // Loading here avoids a copy of available later
}
// Load resource instances
for (int i = 0; i < RESOURCE_COUNT; i++)
{
input >> buffer;
instances[i] = std::stoi(buffer);
}
// Load resource allocation
for (int i = 0; i < PROCESS_COUNT; i++)
{
for (int j = 0; j < RESOURCE_COUNT; j++)
{
input >> buffer;
allocatedResource[i][j] = std::stoi(buffer);
}
}
// Load max resource requirements
for (int i = 0; i < PROCESS_COUNT; i++)
{
for (int j = 0; j < RESOURCE_COUNT; j++)
{
input >> buffer;
maximumResources[i][j] = std::stoi(buffer);
}
}
// Close file
input.close();
if (bankers(work, maximumResources, allocatedResource))
{
std::cout << "SAFE" << std::endl
<< "SAFE SEQUENCE IS" << std::endl;
for (auto &num : safeOrder)
{
std::cout << "P" << num << std::endl;
}
}
else
{
std::cout << "NOT SAFE";
}
}
bool bankers(int work[], int maxRes[][RESOURCE_COUNT], int allocation[][RESOURCE_COUNT])
{
// Initalize array of whether a process has found a spot yet
bool finish[PROCESS_COUNT] = {false, false, false, false, false};
// Needed resources to be allocated
int need[PROCESS_COUNT][RESOURCE_COUNT];
// Calculate what each process needs
for (int i = 0; i < PROCESS_COUNT; i++)
{
for (int j = 0; j < RESOURCE_COUNT; j++)
{
need[i][j] = maxRes[i][j] - allocation[i][j];
}
}
int safeProcesses = 0;
// While there has been no safe sequence found, loop
while (safeProcesses < PROCESS_COUNT)
{
bool safe = false;
for (int i = 0; i < PROCESS_COUNT; i++)
{
//Check process not already accomodated
if (!finish[i])
{
//Initialize counter
int j;
//See if it needs more than can be granted
for (j = 0; j < RESOURCE_COUNT; j++)
{
if (need[i][j] > work[j])
break;
}
// If all needs of process were satisfied.
if (j == RESOURCE_COUNT)
{
// Add resources to work
for (int k = 0; k < RESOURCE_COUNT; k++)
{
work[k] += allocation[i][k];
}
// Add this process to safe sequence.
safeProcesses++;
safeOrder.push_back(i);
//Set process as finished
finish[i] = true;
safe = true;
}
}
}
// If no proc is safe, return false
if (!safe)
{
return false;
}
}
return true;
}