-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cpp
More file actions
369 lines (321 loc) · 11.3 KB
/
InputManager.cpp
File metadata and controls
369 lines (321 loc) · 11.3 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include "InputManager.h"
#include "Process.h" //temporary include for process
using namespace std;
void InputManager::welcomeMessage() {
cout << "****** Welcome to CS452's Lab 2: Process Scheduling Assignment. ******" << endl;
cout << "You will be asked to choose running options below, please only enter inputs 1 or 0. " << endl << endl;
}
void InputManager::setAlgorithmType() {
cout << "Choose the algorithm you would like to run for process scheduling:" << endl;
cout << "Enter \'0\' for a MFQS algorithm, and enter \'1\' for a real time algorithm" << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
if (!goodInput(input)) {
while (!goodInput(input)) {
cout << "Please only input values 1 and 0." << endl;
cout << "Enter \'0\' for a MFQS algorithm, and enter \'1\' for a real time algorithm" << endl;
cout << "Input your choice here: ";
cin >> input;
}
}
algorithmType = input;
};
void InputManager::setReadType() {
cout << endl << "Choose if you would like to read from a file or manually input processes:" << endl;
cout << "Enter \'0\' to read from a file, and enter \'1\' to manually enter processes" << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
if (!goodInput(input)) {
while (!goodInput(input)) {
cout << "Please only input values 1 and 0." << endl;
cout << "Enter \'0\' to read from a file, and enter \'1\' to manually enter processes" << endl;
cout << "Input your choice here: ";
cin >> input;
}
}
readType = input;
}
void InputManager::readFile() {
ifstream inputFile;
int processCount = 0;
string fileLine;
string pidString;
string burstString;
string arrivalString;
string priorityString;
string deadlineString;
string IOString;
// std::vector <Process> processes;
string fileName = "";
cout << "Enter the name of the text file you want to read: ";
cin >> fileName;
inputFile.open(fileName);
getline(inputFile, fileLine); // ignore first line of column headers
if (inputFile.is_open() ) {
string line;
while(getline(inputFile, line)){
int len = line.length();
char lineChars[len+1];
int itemCount = 0;
strcpy(lineChars, line.c_str());
for (int i = 0; i < len; i++) {
if(itemCount == 0) {
if(lineChars[i] != '\t') {
pidString += lineChars[i];
} else {
itemCount = 1;
i++;
}
}
if(itemCount == 1) {
if(lineChars[i] != '\t') {
burstString += lineChars[i];
} else {
itemCount = 2;
i++;
}
}
if(itemCount == 2) {
if(lineChars[i] != '\t') {
arrivalString += lineChars[i];
} else {
itemCount = 3;
i++;
}
}
if(itemCount == 3) {
if(lineChars[i] != '\t') {
priorityString += lineChars[i];
} else {
itemCount = 4;
i++;
}
}
if(itemCount == 4) {
if(lineChars[i] != '\t') {
deadlineString += lineChars[i];
} else {
itemCount = 5;
i++;
}
}
if(itemCount == 5) {
if(lineChars[i] != '\t') {
IOString += lineChars[i];
} else {
itemCount = 6;
i++;
}
}
}
pid = stoi(pidString);
burst = stoi(burstString);
arrival = stoi(arrivalString);
priority = stoi(priorityString);
deadline = stoi(deadlineString);
io = stoi(IOString);
// cout << "pid: " << pid << endl;
// cout << "burst: " << burst << endl;
// cout << "arrival: " << arrival << endl;
// cout << "priority: " << priority << endl;
// cout << "deadline: " << deadline << endl;
// cout << "io: " << io << endl;
Process newProcess = Process(pid, arrival, burst, deadline, priority, io);
if (isSanitized(newProcess)) {
//cout << "pid: " << pid << " is sanitized " <<endl;
processes.push_back(newProcess);
processCount++;
}
//cout << endl;
pidString = "";
burstString = "";
arrivalString = "";
priorityString = "";
deadlineString = "";
IOString = "";
}
}
inputFile.close();
}
void InputManager::readFromUser() {
cout << endl << "You have chosen to input your processes manually." << endl;
cout << "For each process, you will be prompted for burst, arrival, priority, deadline, and I/O." << endl;
// std::vector <Process> processes;
bool exitFlag = false;
pid = 1;
int processCount = 0;
while(!exitFlag) {
//pid++;
cout << "Input Process " << pid << " Burst: ";
cin >> burst;
cout << "Input Process " << pid << " Arrival: ";
cin >> arrival;
cout << "Input Process " << pid << " Priority: ";
cin >> priority;
cout << "Input Process " << pid << " Deadline: ";
cin >> deadline;
cout << "Input Process " << pid << " I/O: ";
cin >> io;
Process newProcess = Process(pid, arrival, burst, deadline, priority, io);
if (isSanitized(newProcess)) {
cout << "pid: " << pid << " is sanitized " << endl;
processes.push_back(newProcess);
processCount++;
pid++;
} else {
cout << "pid: " << pid << " contains invalid input, re-enter the data." << endl;
}
string anotherProcess;
cout << "* * * Would you like to input another process? (\'y\'/\'n\') * * *" << endl;
cin >> anotherProcess;
if (anotherProcess != "y" && anotherProcess != "Y") {
exitFlag = true;
}
anotherProcess = "";
}
}
void InputManager::setRealTimeType() {
cout << endl << "Choose the type of Real Time Algorithm" << endl;
cout << "Enter \'0\' for Soft RT, and enter \'1\' for Hard RT." << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
if (!goodInput(input)) {
while (!goodInput(input)) {
cout << "Please only input values 1 and 0." << endl;
cout << "Enter \'0\' to read from a file, and enter \'1\' to manually enter processes" << endl;
cout << "Input your choice here: ";
cin >> input;
}
}
realTimeType = input;
}
void InputManager::setNumQueues() {
cout << "How many queues would you like MFQS to use (2-5)?" << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
if (input < 2 || input > 5) {
while (input < 2 || input > 5) {
cout << "The number you entered is out of range for number of queues." << endl;
cout << "How many queus would you like MFQS to use (2-5)?" << endl;
cout << "Input your choice here: ";
cin >> input;
}
}
numQueues = input;
}
void InputManager::setTimeQuantum() {
cout << "What time quantum do you want MFQS to use?" << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
timeQuantum = input;
}
void InputManager::setAgeing() {
cout << "What value would you like to use for ageing (when a process should be promoted if it is in the FCFS queue)?" << endl;
cout << "Input your choice here: ";
int input;
cin >> input;
ageing = input;
}
void InputManager::setHandleIO() {
cout << "Do you want to enter an I/O offset?" << endl;
cout << "Enter \'0\' to have NO I/O, and enter \'1\' to have I/O: ";
int input;
cin >> input;
while (!goodInput(input)) {
cout << "Please only input values 1 and 0." << endl;
cout << "Enter \'0\' to NOT enter I/O, and enter \'1\' enter I/O." << endl;
cout << "Input your choice here: ";
cin >> input;
}
if (input == 0) {
handleIO = 0;
} else {
handleIO = 1;
}
}
void InputManager::setIO_Offset() {
cout << endl << "What do you want your I/O offset to be?" << endl;
cout << "Enter an integer here: ";
int input;
cin >> input;
while (input == 0) {
cout << "I/O offset has to be more than 0." << endl;
cout << "Enter an integer here: ";
cin >> input;
}
io_Offset = input;
}
bool InputManager::goodInput(int input) {
if(input == 1 || input == 0) {
return true;
}
return false;
}
bool InputManager::isSanitized(Process process) {
if (process.getArrival() < 0 || process.getPriority() < 0 || process.getIoTime() < 0) {
return false;
}
if (process.getBurst() <= 0 || process.getDeadline() <= 0) {
return false;
}
if (handleIO) {
//cout << "Handling IO" << endl << io_Offset << endl;
if (process.getBurst() < io_Offset) {
return false;
}
}
if(algorithmType == 1) { // If real time algorithm
if(process.getArrival() + process.getBurst() > process.getDeadline()) { //Arrival + burst is more than the deadline specified
return false;
}
}
setMinArrival(process.getArrival());
return true;
}
void InputManager::getInput() {
welcomeMessage();
setAlgorithmType();
if(getAlgorithmType() == 1) { // real time algorithm
setRealTimeType();
} else { // MFQS algorithm
setNumQueues();
setTimeQuantum();
setAgeing();
setHandleIO();
if(getHandleIO() == true) {
setIO_Offset();
}
}
setReadType();
if(getReadType() == 0) {
readFile();
if(processes.size() == 0) {
cout << "Unable to read file; file does not exist or error reading file." << endl;
} else {
cout << "Number of valid processes: " <<processes.size() << endl;
}
sort(processes.begin(), processes.end());
// for (int i = 0; i < processes.size(); i++) {
// cout << "Process " << i << "'s arrival time: " << processes.at(i).getArrival() << endl;
// }
} else {
readFromUser();
cout << "Number of valid processes:: " << processes.size() << endl;
sort(processes.begin(), processes.end());
// for (int i = 0; i < processes.size(); i++) {
// cout << "Process " << i << "'s arrival time: " << processes.at(i).getArrival() << endl;
// }
}
}