-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
294 lines (219 loc) · 8.46 KB
/
Copy pathmain.cpp
File metadata and controls
294 lines (219 loc) · 8.46 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// main.cpp
// cis_450_p2
//
// Created by Peter Schubert on 10/20/20.
// Copyright © 2020 Peter Schubert. All rights reserved.
// Copyright © 2020 Nathan Carey. All rights reserved.
//
// testing that this worked
#include "PCB.h"
#include "MASTAT.h"
#include <algorithm>
vector<double> request_job_distribtution() {
vector<double> job_distro;
double small = 0, medium = 0, large = 0;
bool valid_input = false;
while (!valid_input) {
cout << "Please enter the job distribution for the simulation (small, medium, large)." << endl;
cout << "Small = ";
cin >> small;
cout << endl << "Medium = ";
cin >> medium;
cout << endl << "Large = ";
cin >> large;
if (small + medium + large != 100) {
cout << "ERROR. The distribution did not total 100%!" << endl;
cout << "Please re-enter your values." << endl;
}
else {
valid_input = true;
}
}
// small == index 0, medium == index 1, large == index 2
job_distro.push_back(small);
job_distro.push_back(medium);
job_distro.push_back(large);
return job_distro;
}
int request_sim_duration(const vector<double> job_distro) {
double duration = 0, num_jobs = 0;
int num_small = 0, num_medium = 0, num_large = 0, return_duration = 0;
cout << "Please enter the duration of the simulation: ";
cin >> duration;
// check that duration is valid
while (duration <= 0) {
cout << "ERROR. The duration of the simulation must be greater than 0. Please enter a new distribution." << endl;
cin >> duration;
}
num_jobs = duration / 3;
num_small = (num_jobs * (job_distro[0] / 100));
num_medium = (num_jobs * (job_distro[1] / 100));
num_large = (num_jobs * (job_distro[2] / 100));
while (num_small % 1 != 0 && num_medium % 1 != 0 && num_large % 1 != 0) {
cout << "The distribution did not result whole values for the amount of jobs. Please enter a new distribution: ";
cin >> duration;
// check that duration is valid
while (duration <= 0) {
cout << "ERROR. The duration of the simulation must be greater than 0. Please enter a new distribution." << endl;
cin >> duration;
}
num_jobs = duration / 3;
num_small = (num_jobs * (job_distro[0] / 100));
num_medium = (num_jobs * (job_distro[1] / 100));
num_large = (num_jobs * (job_distro[2] / 100));
}
return_duration = duration;
return return_duration;
}
int request_memory_unit_size() {
int memory_unit_size = 0;
cout << "Please enter the memory unit size that is a multiple of 8. e.g. 8, 16, 24, 32, 40, etc.. " << endl;
cin >> memory_unit_size;
while (memory_unit_size % 8 != 0) {
cout << "ERROR. The memory unit size entered was not a multiple of 8. Please enter a valid memory unit size." << endl;
cin >> memory_unit_size;
}
return memory_unit_size;
}
int request_memory_units_available() {
int memory_units = 0;
cout << "Please enter the amount of memory units available for this simulation. *must be greater than 0." << endl;
cin >> memory_units;
while (memory_units <= 0) {
cout << "ERROR. The amount of memory units available must be greater than 0." << endl;
cin >> memory_units;
}
return memory_units;
}
bool request_lost_objects() {
bool is_lost_objects = false;
string user_input;
cout << "Would you like to simulate lost objects in this simulation? Enter yes or no" << endl;
cin >> user_input;
while (user_input != "yes" && user_input != "no") {
cout << "ERROR. Please enter yes or no if you would like to simulate lost objects or not." << endl;
cin >> user_input;
}
if (user_input == "yes") {
is_lost_objects = true;
}
else if (user_input == "no") {
is_lost_objects = false;
}
return is_lost_objects;
}
string request_algorithm() {
string algo;
cout << "Please enter the algorithm you would like to use for this simulation. Either firstfit, nextfit, bestfit, or worstfit" << endl;
cin >> algo;
while (algo != "firstfit" && algo != "nextfit" && algo != "bestfit" && algo != "worstfit") {
cout << "ERROR. Please enter the name of one of the algorithms for this simulation. Either firstfit, nextfit, bestfit, or worstfit" << endl;
cin >> algo;
}
return algo;
}
// sort here by arrival time
bool compare_arrival_times(job j1, job j2) {
return(j1.job_arrival_time < j2.job_arrival_time);
}
int main() {
// main variables
double small = 0, medium = 0, large = 0, run_time;
priority_queue<job, vector<job>, comparator>queue;
int time_counter, total_memory_available = 0, count_heap_allocations = 0;
vector<int>active_jobs;
vector<heap_elements>active_heap_elements;
// variables to hold method vals
vector<double>distribution; // small == index 0, medium == index 1, large == index 2
int duration = 0, memory_unit_size = 0, num_memory_units = 0;
bool is_lost_objects;
string algorithm_to_run;
// variables for files
string test_name;
string log_file_name, output_file_name;
ofstream log_file, output_file;
cout << "Please enter the name of the simulation: ";
cin >> test_name;
cout << "Please enter the name of the log file for the simulation: ";
cin >> log_file_name;
cout << "Please enter the name of the output file for the simulation: ";
cin >> output_file_name;
distribution = request_job_distribtution();
duration = request_sim_duration(distribution);
memory_unit_size = request_memory_unit_size();
num_memory_units = request_memory_units_available();
is_lost_objects = request_lost_objects();
algorithm_to_run = request_algorithm();
// total memory used for stats class
total_memory_available = memory_unit_size * num_memory_units;
small = distribution[0];
medium = distribution[1];
large = distribution[2];
run_time = duration;
PCB myPCB(small, medium, large, run_time, memory_unit_size, is_lost_objects, log_file_name);
// queue holds jobs before sim begins
myPCB.job_assignments(queue);
// sort here -----------------
// need to sort queue here. Have to add my compare function but waiting to talk to peter becaue I need to add it into PCB i beleive and dont want merge conflicts
MemoryAllocationSystem myMAS(num_memory_units, memory_unit_size, algorithm_to_run);
time_counter = 0;
MASTAT myStats(is_lost_objects, test_name, algorithm_to_run, total_memory_available, num_memory_units, small, medium, large, output_file_name, memory_unit_size);
while (time_counter < duration) {
count_heap_allocations = 0;
try {
// allocate new jobs
for (int i = 0; i < active_jobs.size(); i++) {
myPCB.allocate_and_return_heap_elements(active_jobs[i], myMAS, active_heap_elements, time_counter, count_heap_allocations);
}
//
if (!queue.empty()) {
while (!queue.empty() && queue.top().job_arrival_time == time_counter) {
myPCB.allocate_new_job(queue.top().job_id, myMAS,active_jobs, active_heap_elements, time_counter);
queue.pop();
}
}
}
catch (invalid_argument &message) {
// log/ print something here
cout << message.what() << endl;
break;
}
// !!
// if running for 2000 time units print stats and every 20 after
// !!
if (time_counter % 20 == 0 && time_counter >= 2000) {
myStats.print_statistics(time_counter, myPCB, active_jobs, active_heap_elements, myMAS, algorithm_to_run, count_heap_allocations, time_counter);
}
vector<heap_elements>:: iterator i = active_heap_elements.begin();
while (i != active_heap_elements.end()) {
if (myPCB.check_heap_deallocation(i->job_id, i->element_id, time_counter)) {
if (!myPCB.is_lost_object(i->job_id)) {
myPCB.deallocate_heap(i->job_id, i->element_id, myMAS, time_counter);
}
i=active_heap_elements.erase(i);
}
else{
i++;
}
}
// erase_heap_elements(index_heap_to_remove_from_active_list, active_heap_elements);
vector<int>:: iterator j = active_jobs.begin();
while (j!=active_jobs.end()) {
if (myPCB.check_job_deallocation(*j, time_counter)) {
myPCB.deallocate_job(*j, myMAS, time_counter);
j=active_jobs.erase(j);
}
else{
j++;
}
}
// erase_job(index_job_to_remove_from_active_list, active_jobs);
++time_counter;
}
// print final statistics!!
myStats.print_statistics(time_counter, myPCB, active_jobs, active_heap_elements, myMAS, algorithm_to_run, count_heap_allocations, time_counter);
myStats.close_output();
myPCB.close_log();
return 0;
}