-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cxx
513 lines (500 loc) · 12.3 KB
/
main.cxx
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct process//This is the PCB
{
int pid;
int arrival;
int burstTime;
float startTime;
float finishTime;
int curProcessed;
int remaining;
int queue;
int switches;
bool complete;
bool arrived;
};
//Header Stuff
void FCFS();
void SRTF();
void RR(int quantum);
vector<process> ReadInputFile();
void ScreenWrite(string, vector<process>);
//End Header Stuff
char* fileName;
int main(int argc, char* argv[])
{
//cout << "Entered Main" << endl;
if(argc)
{
if(atoi(argv[2]) == 0)
{
fileName = argv[1];
FCFS();
}
else if(atoi(argv[2]) == 1)
{
fileName = argv[1];
SRTF();
}
else if(atoi(argv[2]) == 2 && argc > 3)
{
fileName = argv[1];
RR(atoi(argv[3]));
}
else
{
cout << "Invalid command please try again" << endl;
}
}
else
{
cout << "Invalid command please try again" << endl;
}
}
vector<process> ReadInputFile()
{
vector<process> group;
int iterator = 0;
int currentSize = 10;
ifstream file;
cout << "Reading File: " << fileName << endl;
file.open(fileName);
if(file.is_open())
{
int read;
while(file >> read)
{
process temp;
temp.pid = read;
file >> read;
temp.arrival = read;
file >> read;
temp.burstTime = read;
temp.startTime = -1;
temp.finishTime = -1;
temp.curProcessed = 0;
temp.remaining = read;
temp.switches = 0;
temp.complete = false;
group.push_back(temp);
}
cout << "Read Complete" << endl;
return group;
}
else
{
cout << "File not found" << endl;
return group;
}
}
void FCFS()
{
bool running = true;
bool allArrived = false;
float moment = 0.0;
vector<process> masterList = ReadInputFile();
if(masterList.size() < 1)
return;
int size = masterList.size();
int current = -1;
int queueSize = 0;
int arrivalCount = 0;
cout << "FCFS Setup Complete Starting Simulation" << endl;
while(running)
{
//Setup and status check
if(arrivalCount < size)//Skip this check if all have arrived.
{
for(int i = 0; i < size; i++)//Check for new arrivals
{
if(masterList[i].arrived != true)
{
if(masterList[i].arrival <= moment)
{
if(queueSize == 0)
{
current = i;
queueSize++;
arrivalCount++;
masterList[i].arrived = true;
}
else
{
masterList[i].queue = queueSize;
queueSize++;
arrivalCount++;
masterList[i].arrived = true;
}
}
}
}
}
else
{
allArrived = true;
}
//cout << "Processing Moment: " << moment << endl;
//Processing
if(current != -1)
{//Skip this if we have nothing running
if(masterList[current].startTime == -1)
{
masterList[current].startTime = moment;
}
masterList[current].curProcessed++;
//cout << "Burst: " << masterList[current].burstTime << endl << "Currently Processed: " << masterList[current].curProcessed << endl;
masterList[current].remaining = masterList[current].burstTime - masterList[current].curProcessed;
//cout << "Remaining: " << masterList[current].remaining << endl;
moment += 1;
if(masterList[current].remaining == 0)
{//if process complete
masterList[current].complete = true;
masterList[current].queue = -1;//This is so that we dont pick this again
masterList[current].finishTime = moment;
queueSize--;
for(int i = 0; i < size; i++)
{
if(!masterList[i].complete)
{
masterList[i].queue--;
}
}
current = -1;
for(int i = 0; i < size; i++)
{
if(masterList[i].queue == 0)
{
current = i;
break;
}
}
if(current == -1 && allArrived)
{
running = false;//completed sim
}
}
}
else
{
moment += 1;
}
}
ScreenWrite("FCFS", masterList);
}
void SRTF()
{
bool running = true;
bool allArrived = false;
bool noJob = false;
float moment = 0.0;
vector<process> masterList = ReadInputFile();
if(masterList.size() < 1)
return;
vector<int> queue;
int size = masterList.size();
int current = -1;
int queueSize = 0;
int arrivalCount = 0;
cout << "SRTF Setup Complete Starting Simulation" << endl;
while(running)
{
//Setup and status check
if(arrivalCount < size)//Skip this check if all have arrived.
{
for(int i = 0; i < size; i++)//Check for new arrivals and reorder the queue
{
if(masterList[i].arrived != true)
{
if(masterList[i].arrival <= moment)
{
if(queueSize == 0)
{
noJob = false;
current = i;
queue.push_back(i);
queueSize++;
arrivalCount++;
masterList[i].queue = 0;
masterList[i].arrived = true;
//cout << masterList[i].pid << " inserted at start" << endl;
}
else
{
bool inserted = false;
for(int j = 0; j < queue.size(); j++)
{
if(masterList[i].remaining < masterList[queue[j]].remaining)
{
//cout << masterList[i].pid << " inserted at " << j << endl;
inserted = true;
queue.insert(queue.begin()+j, i);
break;
}
}
if(!inserted)
{
queue.push_back(i);
//cout << masterList[i].pid << " inserted at end"<< endl;
}
queueSize++;
arrivalCount++;
masterList[i].arrived = true;
}
}
}
}
}
else
{
allArrived = true;
}
//Checking if queue has shifted the current job
if(!noJob && queue[0] != current && masterList[current].queue != -1)
{
//cout << "Context switching ------------------------------------" << endl;
masterList[current].switches++;
current = queue[0];
moment += 0.5;//context switch
}
//cout << "Processing Moment: " << moment << endl;
//Processing
if(current != -1)
{//Skip this if we have nothing running
if(masterList[current].startTime == -1)
{
masterList[current].startTime = moment;
}
masterList[current].curProcessed++;
//cout << "Burst: " << masterList[current].burstTime << endl << "Currently Processed: " << masterList[current].curProcessed << endl;
masterList[current].remaining = masterList[current].burstTime - masterList[current].curProcessed;
//cout << "Remaining: " << masterList[current].remaining << endl;
moment += 1;
if(masterList[current].remaining <= 0)
{//if process complete
masterList[current].complete = true;
masterList[current].queue = -1;//This is so that we dont pick this again
masterList[current].finishTime = moment;
queueSize--;
queue.erase(queue.begin());
//cout << "Job Complete" << endl;
if(queue.size() > 0)
{
current = queue[0];
}
else
{
current = -1;
noJob = true;
}
if(current == -1 && allArrived)
{
running = false;//completed sim
}
}
}
else if(current == -1 && allArrived)
{
running = false;//completed sim
}
else
{
moment += 1;
}
}
ScreenWrite("SRTF", masterList);
}
void RR(int quantum)
{
bool running = true;
bool allArrived = false;
float moment = 0.0;
int quantCount = 0;//This counts how far into the quantum a process is.
vector<process> masterList = ReadInputFile();
if(masterList.size() < 1)
return;
int size = masterList.size();
int current = -1;
int queueSize = 0;
int arrivalCount = 0;
cout << "Round Robin Setup Complete Starting Simulation - Quantum: " << quantum << endl;
while(running)
{
//Setup and status check
if(arrivalCount < size)//Skip this check if all have arrived.
{
for(int i = 0; i < size; i++)//Check for new arrivals
{
if(masterList[i].arrived != true)
{
if(masterList[i].arrival <= moment)
{
if(queueSize == 0)
{
current = i;
queueSize++;
arrivalCount++;
quantCount = 0;
masterList[i].arrived = true;
}
else
{
masterList[i].queue = queueSize;
queueSize++;
arrivalCount++;
masterList[i].arrived = true;
}
}
}
}
}
else
{
allArrived = true;
}
//cout << "Processing Moment: " << moment << endl;
//Processing
if(current != -1)
{//Skip this if we have nothing running
if(masterList[current].startTime == -1)
{
masterList[current].startTime = moment;
}
masterList[current].curProcessed++;//push the processed count
quantCount++;
moment++;
//cout << "Process PID: " << masterList[current].pid << endl;
masterList[current].remaining = masterList[current].burstTime - masterList[current].curProcessed;
//cout << "Remaining: " << masterList[current].remaining << endl;
if(masterList[current].remaining == 0)
{//if process complete
masterList[current].complete = true;
masterList[current].queue = -1;//This is so that we dont pick this again
masterList[current].finishTime = moment;
queueSize--;
for(int i = 0; i < size; i++)
{
if(!masterList[i].complete)
{
masterList[i].queue--;
}
}
current = -1;
for(int i = 0; i < size; i++)
{
if(masterList[i].queue == 0)
{
quantCount = 0;
current = i;
break;
}
}
if(current == -1 && allArrived)
{
running = false;//completed sim
}
}
else if(quantCount >= quantum)//TIMES UP
{
//I would like to note that I did not really want to copy paste this extra bit in here
//But I also did not want to make it into a method because that would have been a lot
//More effort and made this problem harder.
if(arrivalCount < size)//Skip this check if all have arrived.
{
for(int i = 0; i < size; i++)//Check for new arrivals
{
if(masterList[i].arrived != true)
{
if(masterList[i].arrival <= moment)
{
if(queueSize == 0)
{
current = i;
queueSize++;
arrivalCount++;
quantCount = 0;
masterList[i].arrived = true;
}
else
{
masterList[i].queue = queueSize;
queueSize++;
arrivalCount++;
masterList[i].arrived = true;
}
}
}
}
}
else
{
allArrived = true;
}
masterList[current].queue = queueSize;
masterList[current].switches++;
quantCount = 0;
for(int i = 0; i < size; i++)
{
if(!masterList[i].complete)
{
masterList[i].queue--;
if(masterList[i].queue == 0)
current = i;
}
}
moment += 0.5;
}
}
else
{
moment += 1;
}
}
string temp = "Round Robin";
ScreenWrite(temp, masterList);
}
void ScreenWrite(string algo, vector<process> list)
{
int procCount = list.size();
int burstAvg = 0;
int turnAvg = 0;
int respAvg = 0;
int waitAvg = 0;
int switches = 0;
cout << "*********************************************************************"<<endl;
cout << "** " << algo << endl;
cout << "*********************************************************************"<<endl;
cout << " ___________________________________________________________________" <<endl;
cout << "|pid | arrival | burst | finish | waiting| turn- | response | No.of |" <<endl;
cout << "| | | | time | time | around| time |Context|" <<endl;
cout << "|____|_________|_______|________|________|_______|__________|_______|" <<endl;
for(int i = 0; i < list.size(); i++)
{
cout << "| " << left << setw(3) << setfill(' ') << list[i].pid;
cout << "| " << left << setw(8) << setfill(' ') << list[i].arrival;
burstAvg += list[i].burstTime;
cout << "| " << left << setw(6) << setfill(' ') << list[i].burstTime;
cout << "| " << left << setw(7) << setfill(' ') << list[i].finishTime;
int wait = list[i].finishTime - list[i].arrival - list[i].burstTime;
waitAvg += wait;
cout << "| " << left << setw(7) << setfill(' ') << wait;//wait
int turn = list[i].finishTime - list[i].arrival;
turnAvg += turn;
cout << "| " << left << setw(6) << setfill(' ') << turn;//turnaround
int resp = list[i].startTime - list[i].arrival;
respAvg += resp;
cout << "| " << left << setw(9) << setfill(' ') << resp;
switches += list[i].switches;
cout << "| " << left << setw(6) << setfill(' ') << list[i].switches << "|" << endl;
}
cout << "|___________________________________________________________________|" << endl << endl;
cout << "Average CPU Burst Time: " << (burstAvg/procCount) << endl;
cout << "Average Wait Time: " << (waitAvg/procCount) << endl;
cout << "Average TurnAround Time: " << (turnAvg/procCount) << endl;
cout << "Average Response Time: " << (respAvg/procCount) << endl;
cout << "Total Context Switches: " << switches << endl;
}