-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
1500 lines (1037 loc) · 39.9 KB
/
scheduler.cpp
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <streambuf>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
using namespace std;
// declare my methods...
int strToInt (string s);
void sortProcesses(); // sorts processes.
void fileReader(); // reads in file, creates process objects from each process, stores information in quadruples into objects
// Objects are then added to a vector for sorting later.
int randomOS(int burst);
void createRandomNumVector();
void fcfs();
void lcfs();
void roundRobin();
void hprn();
class process;
bool isVerbose = false;
char entireFile[601];
int process_num = 0;
int process_idx = 0;
int file_idx = 0; // Keep track of how many elements total in entireFile.
std::vector<int> randomNumbers;
int randomIndex = -1; // this increments every time a random number is used. Is -1 because it increments before returning the random num
std::vector<process> presort;
std::vector<process> processes;
int CPUutilization = 0; // needs to be reset at the end of each algorithm to prep for the next algo to run
int IOutilization = 0; // same as CPUutilization but for total IO Utilization instead.
int quantum = 2; // can be reset globally.
class process {
private:
int arrivalTime_A;
int cpuBurst_B;
int cpuTime_C;
int IOtime_D;
int arrival_tiebreak; // process's priority number. Equal to arrivalTime_A + position in sorted array.
string current_state;
int timeRemaining; // set to 0 in constructor
int CPUcycles; // set to 0 in constructor
int totalIO; // set to 0 in constructor
int waitingTime; // set to 0 in constructor.
int startTime;
int finishingTime;
int turnaroundTime;
int PID; // this is the process's position in the processes vector.
bool greaterThan;
int remainingQuant;
public:
process(int arrival, int CPUburst, int CPUtime, int IOtime, int TR, int CC, int TIO, int WT);
int getArrival();
void setArrival(int arrival);
int getCPUburst();
void setCPUburst(int CPUburst);
int getCPUTime();
void setCPUTime(int CPUtime);
int getIOtime();
void setIOtime(int IOtime);
int getArrivalTiebreak();
void setArrivalTiebreak(int tiebreak_arrival);
string getCurrent_state();
void setCurrent_state(string state);
int getTimeRemaining();
void setTimeRemaining(int remaining);
int getCPUCycles();
void clearCPUCycles();
void setCPUCycles();
int getTotalIO();
void clearTotalIO();
void setTotalIO(int burst);
int getWaitingTime();
void setWaitingtime(int waiting);
void incrementWaiting();
void clearWaiting();
int getFinishingTime();
void setFinishingTime(int finish);
int getTurnaroundTime();
void setTurnaroundTime();
int getStartTime();
void setStartTime(int start);
int getPID();
void setPID(int numPID);
bool getGreaterThanQuant();
void setGreaterThanQuant(int burst);
int getRemainingQuant();
void setRemainingQuant(int burst);
void decrementRemainingQuant();
};
process::process (int arrival, int CPUburst, int CPUtime, int IOtime, int TR, int CC, int TIO, int WT){
arrivalTime_A = arrival;
cpuBurst_B = CPUburst;
cpuTime_C = CPUtime;
IOtime_D = IOtime;
timeRemaining = TR;
CPUcycles = CC;
totalIO = TIO;
waitingTime = WT;
}
// these are my getters and setters
int process::getRemainingQuant() {
return remainingQuant;
}
void process::decrementRemainingQuant() {
remainingQuant -= quantum;
if (remainingQuant < 0) {
remainingQuant = 0;
}
}
void process::setRemainingQuant(int burst) {
remainingQuant = burst;
}
bool process::getGreaterThanQuant() {
return greaterThan;
}
void process::setGreaterThanQuant(int burst) {
if (burst > 2) {
greaterThan = true;
}
else {
greaterThan = false;
}
}
int process::getPID(){
return PID;
}
void process::setPID(int numPID){
PID = numPID;
}
int process::getStartTime() {
return startTime;
}
void process::setStartTime(int start){
startTime = start;
}
int process::getTurnaroundTime(){
return turnaroundTime;
}
void process::setTurnaroundTime(){
turnaroundTime = finishingTime - arrivalTime_A;
}
int process::getFinishingTime(){
return finishingTime;
}
void process::setFinishingTime(int finish){
finishingTime = finish;
}
void process::clearWaiting() {
waitingTime = 0;
}
int process::getWaitingTime(){
return waitingTime;
}
void process::setWaitingtime(int waiting) {
waitingTime = waiting;
}
void process::incrementWaiting() {
waitingTime++;
}
int process::getTotalIO() {
return totalIO;
}
void process::clearTotalIO() {
totalIO = 0;
}
void process::setTotalIO(int burst){
totalIO += burst;
}
int process::getCPUCycles(){
return CPUcycles;
}
void process::clearCPUCycles() {
CPUcycles = 0;
}
void process::setCPUCycles(){
CPUcycles++;
}
string process::getCurrent_state() {
return current_state;
}
void process::setCurrent_state(string state){
current_state = state;
}
int process::getTimeRemaining() {
return timeRemaining;
}
void process::setTimeRemaining(int remaining){
timeRemaining = remaining;
}
int process::getArrival() {
return arrivalTime_A;
}
void process::setArrival(int arrival){
arrivalTime_A = arrival;
}
int process::getCPUburst(){
return cpuBurst_B;
}
void process::setCPUburst(int CPUburst){
cpuBurst_B = CPUburst;
}
int process::getCPUTime(){
return cpuTime_C;
}
void process::setCPUTime(int CPUtime){
cpuTime_C = CPUtime;
}
int process::getIOtime(){
return IOtime_D;
}
void process::setIOtime(int IOtime){
IOtime_D = IOtime;
}
int process::getArrivalTiebreak(){
return arrival_tiebreak;
}
void process::setArrivalTiebreak(int tiebreak_arrival){
arrival_tiebreak = tiebreak_arrival; // needs to be position in array plus its original arrival time
}
// string to int function
int strToInt (string s) {
int newInt = atoi(s.c_str());
return newInt;
}
void fileReader() {
char fileContent;
while ((fileContent = getchar()) != EOF){
entireFile[file_idx] = fileContent;
file_idx++;
if (file_idx > 600){
cout << "Characters in file exceed 600 char limit. Exiting now..." << endl;
exit(0);
}
}
if (file_idx < 1){
cout << "File is empty. Exiting program now..." << endl;
exit(1);
}
entireFile[file_idx] = '*'; // mark the first empty element in char array as '*'.
// file has been copied to entireFile[].
int idx = 0;
while (entireFile[idx] != '*'){
char c = entireFile[idx];
while((c == ' ') || (c == '\t') || (c == '\n') || (c == '(') || (c == ')')){
idx++;
c = entireFile[idx];
}
string num_processes = "";
while ((c != ' ') && (c != '\t') && (c != '\n') && (c != '(') && (c != ')')){
num_processes += c;
idx++;
c = entireFile[idx];
}
process_num = strToInt(num_processes);
//cout << "process_num: " << process_num << endl;
for (int i = 0; i < process_num; i++){
while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '(') || (c == ')')){
idx++;
c = entireFile[idx];
}
string varA = "";
while ((c != ' ') && (c != '\t') && (c != '\n') && (c != '(') && (c != ')')){
varA += c;
idx++;
c = entireFile[idx];
}
while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '(') || (c == ')')){
idx++;
c = entireFile[idx];
}
string varB = "";
while ((c != ' ') && (c != '\t') && (c != '\n') && (c != '(') && (c != ')')){
varB += c;
idx++;
c = entireFile[idx];
}
while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '(') || (c == ')')){
idx++;
c = entireFile[idx];
}
string varC = "";
while ((c != ' ') && (c != '\t') && (c != '\n') && (c != '(') && (c != ')')){
varC += c;
idx++;
c = entireFile[idx];
}
while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '(') || (c == ')')){
idx++;
c = entireFile[idx];
}
string varD = "";
while ((c != ' ') && (c != '\t') && (c != '\n') && (c != '(') && (c != ')')){
varD += c;
idx++;
c = entireFile[idx];
}
process temp(strToInt(varA), strToInt(varB), strToInt(varC), strToInt(varD), 0, 0, 0, 0);
processes.push_back(temp);
} // end for loop
break;
} // end outer while loop
} // end fileReader
void sortProcesses () {
//process sort_array[process_num];
std::vector<process> sortVector;
for (int i = 0; i < process_num; i++){
presort.push_back(processes.front());
processes.erase(processes.begin()); // pops the front of the processes
}
for (int i = 0 ; i < presort.size() ; i++) {
sortVector.insert(sortVector.begin()+i,presort.at(i));
}
for (int i = (process_num - 1); i > 0; i--) {
for (int j = (i - 1); j >= 0; j--){
if (sortVector[i].getArrival() < sortVector[j].getArrival()){
process tmp = sortVector[i];
sortVector[i] = sortVector[j];
sortVector[j] = tmp;
}
}
}
for (int i = 0; i < process_num; i++){
sortVector[i].setArrivalTiebreak(i);
}
for (int j = 0; j < process_num - 1; j++) {
for (int i = j + 1; i < (process_num); i++) {
if (sortVector[j].getArrival() < sortVector[i].getArrival()) {
continue;
}
else {
if ((sortVector[i].getCPUburst() < sortVector[j].getCPUburst()) || (sortVector[i].getCPUburst() == sortVector[j].getCPUburst()) && (sortVector[i].getCPUTime() < sortVector[j].getCPUTime())
|| (sortVector[i].getCPUburst() == sortVector [j].getCPUburst()) && (sortVector[i].getCPUTime() == sortVector[j].getCPUTime()) && (sortVector[i].getIOtime() < sortVector[j].getIOtime())) {
process tmp = sortVector[i];
sortVector[i] = sortVector[j];
sortVector[j] = tmp;
}
// else if ((sortVector[i].getCPUburst() == sortVector[j].getCPUburst()) && (sortVector[i].getCPUTime() < sortVector[j].getCPUTime())) {
// process tmp(sortVector[i].getArrival(), sortVector[i].getCPUburst(), sortVector[i].getCPUTime(), sortVector[i].getIOtime(),0, 0, 0, 0);
// sortVector[i] = sortVector[j];
// sortVector[j] = tmp;
// }
// else if ((sortVector[i].getCPUburst() == sortVector [j].getCPUburst()) && (sortVector[i].getCPUTime() == sortVector[j].getCPUTime()) && (sortVector[i].getIOtime() < sortVector[j].getIOtime())) {
// process tmp(sortVector[i].getArrival(), sortVector[i].getCPUburst(), sortVector[i].getCPUTime(), sortVector[i].getIOtime(),0, 0, 0, 0);
// sortVector[i] = sortVector[j];
// sortVector[j] = tmp;
// }
}
}
}
for (int i = 0; i < process_num; i++){
sortVector[i].setPID(i);
//sortVector[i].setArrivalTiebreak(i);
processes.push_back(sortVector[i]);
}
} // end sortProcesses
void createRandomNumVector(){
ifstream infile;
infile.open("random-numbers.txt");
int d;
while (infile >> d){
randomNumbers.push_back(d);
}
} // end createRandomNumVector
int randomOS(int burst){
randomIndex++;
int randomNumber = ((randomNumbers[randomIndex])%burst) + 1;
return randomNumber;
} // end randomOS
void fcfs() {
int terminated = 0;
randomIndex = -1;
int running_process = -1; // indicates that there are no current processes running
std::deque<int> ready_process;
std::deque<int> blocked_process;
int cycleNum = 0;
for (int i = 0; i < process_num; i++) {
processes[i].setCurrent_state("unstarted");
processes[i].clearTotalIO();
processes[i].clearCPUCycles();
processes[i].clearWaiting();
}
cout << "The original input was: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << presort[i].getArrival() << " " << left << presort[i].getCPUburst() << " " << left << presort[i].getCPUTime() << " " << left << presort[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << "The (sorted) input is: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << processes[i].getArrival() << " " << left << processes[i].getCPUburst() << " " << left << processes[i].getCPUTime() << " " << left << processes[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << '\n';
if (isVerbose) {
cout << "This detailed printout gives the state and remaining burst for each process " << endl;
}
cout << '\n';
while (terminated < process_num) {
if (isVerbose) {
// cout << "Before cycle" << setw(5) << cycleNum << ":";
// for (int i = 0; i < process_num; i++){
// cout << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << processes[i].getTimeRemaining();
// }
// cout <<"."<< endl;
stringstream buffer;
buffer << "Before cycle" << setw(5) << cycleNum << ":";
for (int i = 0; i < process_num; i++) {
buffer << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << processes[i].getTimeRemaining();
}
buffer << ".";
cout << buffer.str() << endl;
}
for (int i = 0; i < blocked_process.size(); i++) {
int getTR = processes[blocked_process[i]].getTimeRemaining();
processes[blocked_process[i]].setTimeRemaining(getTR - 1);
if (processes[blocked_process[i]].getTimeRemaining() == 0) {
processes[blocked_process[i]].setCurrent_state("ready");
ready_process.push_back(blocked_process[i]);
blocked_process.erase(blocked_process.begin() + i);
i--;
}
} // end blocked
// unstarted and arriving in this cycle, put them into ready for now
// will check for running later in the loop
for (int i = 0; i < process_num; i++){
if ((processes[i].getCurrent_state() == "unstarted") && (processes[i].getArrival() == cycleNum)) {
//cout << "in unstarted loop" << endl;
processes[i].setCurrent_state("ready");
ready_process.push_back(i);
}
} // end for loop checking arrivals
if (running_process > -1) {
processes[running_process].setTimeRemaining(processes[running_process].getTimeRemaining() - 1);
processes[running_process].setCPUCycles();
if (processes[running_process].getCPUCycles() == processes[running_process].getCPUTime()) {
processes[running_process].setFinishingTime(cycleNum);
processes[running_process].setTurnaroundTime();
processes[running_process].setTimeRemaining(0);
processes[running_process].setCurrent_state("terminated");
terminated++;
running_process = -1;
}
else if (processes[running_process].getTimeRemaining() == 0) {
int IOBurst = randomOS(processes[running_process].getIOtime());
processes[running_process].setTotalIO(IOBurst);
processes[running_process].setTimeRemaining(IOBurst);
processes[running_process].setCurrent_state("blocked");
bool inBlocked = false;
//blocked_process.push_back(running_process);
for (int i = 0; i < blocked_process.size(); i++) {
if (processes[running_process].getArrivalTiebreak() < processes[blocked_process[i]].getArrivalTiebreak()) {
blocked_process.insert(blocked_process.begin() + i, running_process);
inBlocked = true;
break;
}
if (processes[running_process].getArrivalTiebreak() == processes[blocked_process[i]].getArrivalTiebreak()) {
cout << "Arrival tie breaker match" << endl;
}
}
if (!inBlocked) {
blocked_process.push_back(running_process);
}
running_process = -1;
}
}
if (running_process == -1 && !ready_process.empty()) {
//cout << "inside of runing process check for -1" << endl;
running_process = ready_process.front();
processes[running_process].setTimeRemaining(randomOS(processes[running_process].getCPUburst()));
processes[running_process].setCurrent_state("running");
ready_process.pop_front();
}
for (int i = 0; i < ready_process.size(); i++) {
processes[ready_process[i]].incrementWaiting();
}
if (running_process > -1) {
CPUutilization++;
}
if (!blocked_process.empty()) {
IOutilization++;
}
cycleNum++;
} // end outer while loop
cout << "The scheduling algorithm used was First Come First Served" << endl;
cout << '\n';
// now print stats for each cycle.
int totalTurnaround = 0;
int totalWait = 0;
for (int i = 0; i < process_num; i++) {
cout << "Process " << i << ":" << endl;
cout << '\t' << "(A,B,C,IO) = (" << processes[i].getArrival() << "," << processes[i].getCPUburst() << "," << processes[i].getCPUTime() << "," << processes[i].getIOtime() << ")" << endl;
cout << '\t' << "Finishing time: " << processes[i].getFinishingTime() << endl;;
cout << '\t' << "Turnaround time: " << processes[i].getTurnaroundTime() << endl;
cout << '\t' << "I/O time: " << processes[i].getTotalIO() << endl;
cout << '\t' << "Waiting time: " << processes[i].getWaitingTime() << endl;
cout << '\n';
totalTurnaround += processes[i].getTurnaroundTime();
totalWait += processes[i].getWaitingTime();
}
// now print Summary Data
cout << "Summary Data: " << endl;
cout << '\t' << "Finishing Time: " << cycleNum - 1 << endl;
cout << '\t' << "CPU Utilization: " << ((double) CPUutilization/(cycleNum - 1)) << endl;
cout << '\t' << "I/O Utilization: " << ((double) IOutilization/(cycleNum - 1)) << endl;
cout << '\t' << "Throughput: " << (process_num * ((double) 100 / (cycleNum - 1))) << endl;
cout << '\t' << "Average turnaround time: " << ((double) totalTurnaround/process_num) << endl;
cout << '\t' << "Average waiting time: " << ((double) totalWait / process_num) << endl;
// reset everything: randomIndex, CPU utilization, IO utilization, etc.
randomIndex = -1;
CPUutilization = 0;
IOutilization = 0;
} // end fcfs
void lcfs() {
randomIndex = -1;
int terminated = 0;
int running_process = -1; // indicates that there are no current processes running
std::deque<int> ready_process;
std::deque<int> blocked_process;
int cycleNum = 0;
for (int i = 0; i < process_num; i++) {
processes[i].setCurrent_state("unstarted");
processes[i].clearTotalIO();
processes[i].clearCPUCycles();
processes[i].clearWaiting();
}
cout << "The original input was: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << presort[i].getArrival() << " " << left << presort[i].getCPUburst() << " " << left << presort[i].getCPUTime() << " " << left << presort[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << "The (sorted) input is: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << processes[i].getArrival() << " " << left << processes[i].getCPUburst() << " " << left << processes[i].getCPUTime() << " " << left << processes[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << '\n';
if (isVerbose) {
cout << "This detailed printout gives the state and remaining burst for each process " << endl;
}
cout << '\n';
while (terminated < process_num) {
if (isVerbose) {
cout << "Before cycle" << setw(5) << cycleNum << ":";
for (int i = 0; i < process_num; i++){
cout << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << processes[i].getTimeRemaining();
}
cout <<"."<< endl;
}
for (int i = 0; i < blocked_process.size(); i++) {
int getTR = processes[blocked_process[i]].getTimeRemaining();
processes[blocked_process[i]].setTimeRemaining(getTR - 1);
if (processes[blocked_process[i]].getTimeRemaining() == 0) {
processes[blocked_process[i]].setCurrent_state("ready");
ready_process.push_back(blocked_process[i]); // changed here
blocked_process.erase(blocked_process.begin() + i);
i--;
}
} // end blocked
// unstarted and arriving in this cycle, put them into ready for now
// will check for running later in the loop
for (int i = 0; i < process_num; i++){
if ((processes[i].getCurrent_state() == "unstarted") && (processes[i].getArrival() == cycleNum)) {
//cout << "in unstarted loop" << endl;
processes[i].setCurrent_state("ready");
ready_process.push_front(i);
}
} // end for loop checking arrivals
if (running_process > -1) {
processes[running_process].setTimeRemaining(processes[running_process].getTimeRemaining() - 1);
processes[running_process].setCPUCycles();
if (processes[running_process].getCPUCycles() == processes[running_process].getCPUTime()) {
processes[running_process].setFinishingTime(cycleNum);
processes[running_process].setTurnaroundTime();
processes[running_process].setTimeRemaining(0);
processes[running_process].setCurrent_state("terminated");
terminated++;
running_process = -1;
}
else if (processes[running_process].getTimeRemaining() == 0) {
int IOBurst = randomOS(processes[running_process].getIOtime());
processes[running_process].setTotalIO(IOBurst);
processes[running_process].setTimeRemaining(IOBurst);
processes[running_process].setCurrent_state("blocked");
bool inBlocked = false;
//blocked_process.push_back(running_process);
for (int i = 0; i < blocked_process.size(); i++) {
if (processes[running_process].getArrivalTiebreak() < processes[blocked_process[i]].getArrivalTiebreak()) {
blocked_process.insert(blocked_process.begin() + i, running_process);
inBlocked = true;
break;
}
if (processes[running_process].getArrivalTiebreak() == processes[blocked_process[i]].getArrivalTiebreak()) {
cout << "Arrival tie breaker match" << endl;
}
}
if (!inBlocked) {
blocked_process.push_back(running_process);
}
running_process = -1;
}
}
if (running_process == -1 && !ready_process.empty()) {
//cout << "inside of runing process check for -1" << endl;
running_process = ready_process.back();
processes[running_process].setTimeRemaining(randomOS(processes[running_process].getCPUburst()));
processes[running_process].setCurrent_state("running");
ready_process.pop_back();
}
for (int i = 0; i < ready_process.size(); i++) {
processes[ready_process[i]].incrementWaiting();
}
if (running_process > -1) {
CPUutilization++;
}
if (!blocked_process.empty()) {
IOutilization++;
}
cycleNum++;
} // end outer while loop
cout << "The scheduling algorithm used was Last Come First Served" << endl;
cout << '\n';
// now print stats for each cycle.
int totalTurnaround = 0;
int totalWait = 0;
for (int i = 0; i < process_num; i++) {
cout << "Process " << i << ":" << endl;
cout << '\t' << "(A,B,C,IO) = (" << processes[i].getArrival() << "," << processes[i].getCPUburst() << "," << processes[i].getCPUTime() << "," << processes[i].getIOtime() << ")" << endl;
cout << '\t' << "Finishing time: " << processes[i].getFinishingTime() << endl;;
cout << '\t' << "Turnaround time: " << processes[i].getTurnaroundTime() << endl;
cout << '\t' << "I/O time: " << processes[i].getTotalIO() << endl;
cout << '\t' << "Waiting time: " << processes[i].getWaitingTime() << endl;
cout << '\n';
totalTurnaround += processes[i].getTurnaroundTime();
totalWait += processes[i].getWaitingTime();
}
// now print Summary Data
cout << "Summary Data: " << endl;
cout << '\t' << "Finishing Time: " << cycleNum - 1 << endl;
cout << '\t' << "CPU Utilization: " << ((double) CPUutilization/(cycleNum - 1)) << endl;
cout << '\t' << "I/O Utilization: " << ((double) IOutilization/(cycleNum - 1)) << endl;
cout << '\t' << "Throughput: " << (process_num * ((double) 100 / (cycleNum - 1))) << endl;
cout << '\t' << "Average turnaround time: " << ((double) totalTurnaround/process_num) << endl;
cout << '\t' << "Average waiting time: " << ((double) totalWait / process_num) << endl;
// reset everything: randomIndex, CPU utilization, IO utilization, etc.
randomIndex = -1;
CPUutilization = 0;
IOutilization = 0;
} // end lcfs
void roundRobin() {
int current_quant = 0;
int terminated = 0;
randomIndex = -1;
int running_process = -1; // indicates that there are no current processes running
std::deque<int> ready_process;
std::deque<int> blocked_process;
int cycleNum = 0;
for (int i = 0; i < process_num; i++) {
processes[i].setCurrent_state("unstarted");
processes[i].clearTotalIO();
processes[i].clearCPUCycles();
processes[i].clearWaiting();
}
cout << "The original input was: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << presort[i].getArrival() << " " << left << presort[i].getCPUburst() << " " << left << presort[i].getCPUTime() << " " << left << presort[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << "The (sorted) input is: " << setw(3) << left << process_num << '\t';
for (int i = 0; i < process_num; i++) {
cout << processes[i].getArrival() << " " << left << processes[i].getCPUburst() << " " << left << processes[i].getCPUTime() << " " << left << processes[i].getIOtime() << '\t' << left;
}
cout << '\n';
cout << '\n';
if (isVerbose) {
cout << "This detailed printout gives the state and remaining burst for each process " << endl;
}
cout << '\n';
while (terminated < process_num) {
if (isVerbose) {
// cout << "Before cycle" << setw(5) << cycleNum << ":";
// for (int i = 0; i < process_num; i++){
// cout << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << processes[i].getTimeRemaining();
// }
// cout <<"."<< endl;
stringstream buffer;
buffer << "Before cycle" << setw(5) << cycleNum << ":";
for (int i = 0; i < process_num; i++) {
if (processes[i].getCurrent_state() == "running") {
buffer << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << current_quant;
}
else if (processes[i].getCurrent_state() == "ready") {
buffer << '\t' << setw(5) << right << processes[i].getCurrent_state() << setw(3) << right << 0;