/
Cpu.cpp
903 lines (878 loc) · 27.6 KB
/
Cpu.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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <deque>
#include <cctype>
using namespace std;
#define NUM_ALGORITHMS 5
const string ALGORITHM[NUM_ALGORITHMS] = {"FCFS", "NPSJF", "PSJF", "RR", "RRP"};
enum algorithm {
FCFS, NPSJF, PSJF, RR, RRP
};
typedef struct {
int arrival; //arrival time
int burst; //cpu burst length
} process;
typedef struct {
int waiting;
int turnAround;
} processStats;
//process block
// -- useage: groups a process together with its recorded stats
typedef struct {
process p;
processStats s;
} processBlock;
//cpu scheduling options
typedef struct {
algorithm alg;
int slice; //length of time slice
int prioritySlice; //lengh of priority time slice (RRP only)
int switchTime; //time it takes to perform a contet switch
} option;
void fcfs(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats);
void npsjf(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats);
void psjf(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats);
void rr(vector<process> ps, int slice, int switchTime, int & totalTime, int & idleTime, vector<processStats> & pStats);
void rrp(vector<process> ps, int slice, int prioritySlice, int switchTime, int & totalTime, int & idleTime, vector<processStats> & pStats);
void addProcessByArrival(process & p, vector<process> & ps);
void addProcessBlockByBurst(processBlock & b, deque<processBlock> & bs);
void addNewArrivals(vector<process> & ps, deque<processBlock> & ready);
void addNewArrivalsInOrder(vector<process> & ps, deque<processBlock> & ready);
void printReport(const vector<option> & opts, const vector< vector<processStats> > & pStats, const vector<int> & totalTimes, const vector<int> & idleTimes);
void readInProcesses(string filename, vector<process> & ps);
void readInOptions(string filename, vector<option> & opts);
int main(int argc, char *argv[])
{
vector<process> processes;
vector<option> options;
readInProcesses("P.dat", processes);
readInOptions("S.dat", options);
vector< vector<processStats> > pStats(options.size(), vector<processStats>());
vector<int> totalTimes(options.size(), 0);
vector<int> idleTimes(options.size(), 0);
for(int i=0; i<options.size(); i++)
{
switch (options[i].alg)
{
case FCFS:
fcfs(processes, totalTimes[i], idleTimes[i], pStats[i]);
break;
case NPSJF:
npsjf(processes, totalTimes[i], idleTimes[i], pStats[i]);
break;
case PSJF:
psjf(processes, totalTimes[i], idleTimes[i], pStats[i]);
break;
case RR:
rr(processes, options[i].slice, options[i].switchTime, totalTimes[i], idleTimes[i], pStats[i]);
break;
case RRP:
rrp(processes, options[i].slice, options[i].prioritySlice, options[i].switchTime, totalTimes[i], idleTimes[i], pStats[i]);
break;
default:
break;
}
}
printReport(options, pStats, totalTimes, idleTimes);
}
/* Function: fcfs
* Usage: int totalTime;
* int idleTime
* vector<processStats> pStats;
* fcfs(ps, totalTime, idleTime, pStats);
* -------------------------------------------
* Runs a simulation of the FCFS scheduling algorithm.
* - ps: contains the processes to schedule and execute
* - totalTime: set to equal the total time of execution
* - idleTime: set to equal the total time the cpu is idle
* - pStats: set to contain the timing statistics for each process
*/
void fcfs(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats)
{
pStats.clear();
totalTime = 0;
idleTime = 0;
deque<processBlock> ready;
processBlock cpu;
bool running = false;
while(running || ps.size()+ready.size()>0)
{
/* there are no processes currently in the system */
if(!running && ready.size()==0 && totalTime<ps[0].arrival)
{
idleTime++;
totalTime++;
}
/**/
else
{
/* add any arriving processes to the end of the ready queue */
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
/**/
/* if the cpu is idle, move the next process in the ready queue onto the cpu for running */
if(!running)
{
cpu = ready.front();
ready.pop_front();
running = true;
}
/**/
/* increment times for processes in the ready queue */
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
/**/
/* run the current process 1 time unit */
cpu.p.burst--;
cpu.s.turnAround++;
totalTime++;
if(cpu.p.burst == 0) //if the process is finished, save its timing stats
{
pStats.push_back(cpu.s);
running = false;
}
/**/
}
}
}
/* Function: npsjf
* Usage: int totalTime;
* int idleTime;
* vector<processStats> pStats;
* npsjf(ps, totalTime, idleTime, pStats);
* -------------------------------------------
* Runs a simulation of the NPSJF scheduling algorithm.
* - ps: contains the processes to schedule and execute
* - totalTime: set to equal the total time of execution
* - idleTime: set to equal the total time the cpu is idle
* - pStats: set to contain the timing statistics for each process
*/
void npsjf(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats)
{
pStats.clear();
totalTime = 0;
idleTime = 0;
deque<processBlock> ready;
processBlock cpu;
bool running = false;
while(running || ps.size()+ready.size()>0)
{
/* there are no processes currently in the system */
if(!running && ready.size()==0 && totalTime<ps[0].arrival)
{
idleTime++;
totalTime++;
}
/**/
else
{
/* add any arriving processes to the appropriate position in the ready queue*/
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivalsInOrder(ps, ready);
}
/**/
/* if the cpu is idle, move the next process in the ready queue onto the cpu for running */
if(!running)
{
cpu = ready.front();
ready.pop_front();
running = true;
}
/**/
/* increment times for processes in the ready queue*/
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
/**/
/* run the current process for 1 time unit */
cpu.p.burst--;
cpu.s.turnAround++;
totalTime++;
if(cpu.p.burst==0) //if the process is finished, save its timing stats
{
pStats.push_back(cpu.s);
running = false;
}
/**/
}
}
}
/* Function: psjf
* Usage: int totalTime;
* int idleTime;
* vector<processStats> pStats;
* psjf(ps, totalTime, idleTime, pStats);
* -------------------------------------------
* Runs a simulation of the PSJF scheduling algorithm.
* - ps: contains the processes to schedule and execute
* - totalTime: set to equal the total time of execution
* - idleTime: set to equal the total time the cpu is idle
* - pStats: set to contain the timing statistics for each process
*/
void psjf(vector<process> ps, int & totalTime, int & idleTime, vector<processStats> & pStats)
{
pStats.clear();
totalTime = 0;
idleTime = 0;
deque<processBlock> ready;
processBlock cpu;
bool running = false;
while(running || ps.size()+ready.size()>0)
{
/* there are no processes currently in the system */
if(!running && ready.size()==0 && totalTime<ps[0].arrival)
{
idleTime++;
totalTime++;
}
/**/
else
{
/* add any arriving processes to the appropriate position in the ready queue*/
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivalsInOrder(ps, ready);
/* preempt the current process if a new arrival has a shorter burst */
if(running && cpu.p.burst>ready[0].p.burst)
{
addProcessBlockByBurst(cpu, ready);
cpu = ready.front();
ready.pop_front();
}
/**/
}
/**/
/* if the cpu is idle, move the next process in the ready queue onto the cpu for running */
if(!running)
{
cpu = ready.front();
ready.pop_front();
running = true;
}
/**/
/* increment times for processes in the ready queue*/
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
/**/
/* run the current process for 1 time unit */
cpu.p.burst--;
cpu.s.turnAround++;
totalTime++;
if(cpu.p.burst == 0) //if the process is finished, save its timing stats
{
pStats.push_back(cpu.s);
running = false;
}
/**/
}
}
}
/* Function: rr
* Usage: int switchTime;
* int totalTime;
* int idleTime;
* vector<processStats> pStats;
* rr(ps, slice, switchTime, totalTime, idleTime, pStats);
* -------------------------------------------
* Runs a simulation of the RR scheduling algorithm.
* - ps: contains the processes to schedule and execute
* - slice: the time quantum each process receives
* - switchTime: the time it takes to switch processes
* - totalTime: set to equal the total time of execution
* - idleTime: set to equal the total time the cpu is idle
* - pStats: set to contain the timing statistics for each process
*/
void rr(vector<process> ps, int slice, int switchTime, int & totalTime, int & idleTime, vector<processStats> & pStats)
{
pStats.clear();
totalTime = 0;
idleTime = 0;
deque<processBlock> ready;
int timeRunning = 0;
processBlock cpu;
bool running = false;
while(running || ps.size()+ready.size()>0)
{
/* there are no processes currently in the system */
if(!running && ready.size()==0 && totalTime<ps[0].arrival)
{
idleTime++;
totalTime++;
}
/**/
else
{
/* add any arriving processes to the end of the ready queue */
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
/**/
/* if the cpu is idle, move the next process in the ready queue onto the cpu for running */
if(!running)
{
/* increment times by the context switch time;
* it is assumed that context switch time only applies when swapping in*/
for(int i=0; i<switchTime; i++)
{
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
idleTime++;
totalTime++;
/* add any arriving processes to the end of the ready queue */
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
/**/
}
/**/
cpu = ready.front();
ready.pop_front();
running = true;
timeRunning = 0;
}
/**/
/* increment times for processes in the ready queue*/
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
/**/
/* run the current process for 1 time unit */
cpu.p.burst--;
cpu.s.turnAround++;
timeRunning++;
totalTime++;
if(cpu.p.burst==0) //if the process is finished, save its timing stats
{
pStats.push_back(cpu.s);
running=false;
}
else if(timeRunning==slice) //if the process has used up its time slice, preempt it
{
//add any arriving processes to the end of the ready queue ahead of the preempted process
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
ready.push_back(cpu);
running = false;
}
/**/
}
}
}
/* Function: rrp
* Usage: int switchTime;
* int totalTime;
* int idleTime;
* vector<processStats> pStats;
* rrp(ps, slice, int prioritySlice, switchTime, totalTime, idleTime, pStats);
* -------------------------------------------
* Runs a simulation of the RRP (round robin priority) scheduling algorithm.
* - ps: contains the processes to schedule and execute
* - slice: the time quantum each process receives
* - prioritySlice: if a process has burst<prioritySlice, it runs to completion
* - switchTime: the time it takes to switch processes
* - totalTime: set to equal the total time of execution
* - idleTime: set to equal the total time the cpu is idle
* - pStats: set to contain the timing statistics for each process
*/
void rrp(vector<process> ps, int slice, int prioritySlice, int switchTime, int & totalTime, int & idleTime, vector<processStats> & pStats)
{
pStats.clear();
totalTime = 0;
idleTime = 0;
deque<processBlock> ready;
int timeRunning = 0;
processBlock cpu;
bool running = false;
int currentSlice = slice;
while(running || ps.size()+ready.size()>0)
{
/* there are no processes currently in the system */
if(!running && ready.size()==0 && totalTime<ps[0].arrival)
{
idleTime++;
totalTime++;
}
/**/
else
{
/* add any arriving processes to the end of the ready queue */
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
/**/
/* if the cpu is idle, move the next process in the ready queue onto the cpu for running */
if(!running)
{
/* increment times by the context switch time;
* it is assumed that context switch time only applies when swapping in*/
for(int i=0; i<switchTime; i++)
{
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
idleTime++;
totalTime++;
/* add any arriving processes to the end of the ready queue */
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
/**/
}
/**/
cpu = ready.front();
ready.pop_front();
running = true;
timeRunning = 0;
/* give the process priority if it is eligible */
if(cpu.p.burst <= prioritySlice) currentSlice=cpu.p.burst;
else currentSlice = slice;
/**/
}
/**/
/* increment times for processes in the ready queue*/
for(int i=0; i<ready.size(); i++)
{
ready[i].s.waiting++;
ready[i].s.turnAround++;
}
/**/
/* run the current process for 1 time unit */
cpu.p.burst--;
cpu.s.turnAround++;
timeRunning++;
totalTime++;
if(cpu.p.burst==0) //if the process is finished, save its timing stats
{
pStats.push_back(cpu.s);
running=false;
}
else if(timeRunning==currentSlice) //if the process has used up its time slice, preempt it
{
//add any arriving processes to the end of the ready queue ahead of the preempted process
if(ps.size()>0 && totalTime==ps[0].arrival)
{
addNewArrivals(ps, ready);
}
ready.push_back(cpu);
running = false;
}
/**/
}
}
}
/* Function: addProcessByArrival
* Usage: vector<process> ps
* addProcessByArrival(p, ps);
* -------------------------------------------
* Adds the process in the apporpriate position in the vector,
* keeps it sorted by arrival time from least to greatest.
*/
void addProcessByArrival(process & p, vector<process> & ps)
{
if(ps.size()==0 || p.arrival >= ps.back().arrival )
ps.push_back(p);
else
{
int i = (int)ps.size()-1;
ps.resize(ps.size()+1);
do
{
ps[i+1] = ps[i];
i--;
} while(i>=0 && p.arrival<ps[i].arrival);
ps[i+1] = p;
}
}
/* Function: addProcessBlockByBurst
* Usage: deque<processBlock> p
* addProcessBlockByBurst(b, bs);
* -------------------------------------------
* Adds the processBlock in the apporpriate position in the vector,
* keeps it sorted by the processescpu burst time from least to greatest.
*/
void addProcessBlockByBurst(processBlock & b, deque<processBlock> & bs)
{
if(bs.size()==0 || b.p.burst >= bs.back().p.burst )
bs.push_back(b);
else
{
int i = (int)bs.size()-1;
bs.resize(bs.size()+1);
do
{
bs[i+1] = bs[i];
i--;
} while(i>=0 && b.p.burst<bs[i].p.burst);
bs[i+1] = b;
}
}
/* Function: addNewArrivals
* Usage: deque<processBlock> bs
* addNewArrivals(ps, ready);
* -------------------------------------------
* For all processes in 'ps' that have the same arrival time as 'ps[0]',
* a new 'processBlock' is created, initialized, and inserted at the end of 'ready'.
*/
void addNewArrivals(vector<process> & ps, deque<processBlock> & ready)
{
int arrive = ps[0].arrival;
do
{
processBlock b;
b.p = ps[0];
b.s = processStats();
ready.push_back(b);
ps.erase(ps.begin());
} while(ps.size()>0 && ps[0].arrival==arrive);
}
/* Function: addNewArrivalsInOrder
* Usage: deque<processBlock> bs
* addNewArrivalsInOrder(ps, ready);
* -------------------------------------------
* For all processes in 'ps' that have the same arrival time as 'ps[0]',
* a new 'processBlock' is created, initialized, and inserted at the correct position in 'ready'.
*/
void addNewArrivalsInOrder(vector<process> & ps, deque<processBlock> & ready)
{
int arrive = ps[0].arrival;
do
{
processBlock b;
b.p = ps[0];
b.s = processStats();
addProcessBlockByBurst(b, ready);
ps.erase(ps.begin());
} while(ps.size()>0 && ps[0].arrival==arrive);
}
/* Function: printReport
* Usage: printReport(opts, pStats, totalTimes, idleTimes);
* -------------------------------------------
* Prints out the results of multiple cpu scheduling option simulations.
* - opts: contains all of the simulated cpu scheduling options
* - pStats: a 2d vector where each row contains the processStats for each simulated cpu scheduling option
* - totalTimes: contains the total running time for each simulated cpu scheduling option
* - idleTimes: contains the cpu idle time for each simulated cpu scheduling option
*/
void printReport(const vector<option> & opts, const vector< vector<processStats> > & pStats, const vector<int> & totalTimes, const vector<int> & idleTimes)
{
stringstream ss;
int w = 13; //static column width
int ww = 13; //dynamic column width (Scheduler is the dynamic column)
bool toLong; //is the text to long for the dynamic column
do
{
ss.str("");
toLong = false;
ss << left;
ss << setw(ww) << "" << setw(w) << "Average" << setw(w) << "Average" << setw(w) << "CPU" << endl;
ss << setw(ww) << "" << setw(w) << "Turnaround" << setw(w) << "CPU Waiting" << setw(w) << "Utilization" << endl;
ss << setw(ww) << "Scheduler" << setw(w) << "Time" << setw(w) << "Time" << setw(w) << "%" << endl;
for(int i=0; i<ww+w+w+w-2; i++) ss << "="; //insert a line of the appropriate length
ss << endl;
/* process the results for each scheduling option */
for(int i=0; i<opts.size(); i++)
{
int totalTurnAround=0;
int totalWaiting=0;
for(int j = 0; j<pStats[i].size(); j++)
{
totalTurnAround += pStats[i][j].turnAround;
totalWaiting += pStats[i][j].waiting;
}
double avgTurnAround = (double)totalTurnAround/pStats[i].size();
double avgWaiting = (double)totalWaiting/pStats[i].size();
double cpuUtilization = ( (totalTimes[i]-idleTimes[i])/(double)totalTimes[i] ) * 100;
string scheduler = ALGORITHM[opts[i].alg];
if(opts[i].alg==RR) scheduler += "-" + to_string(opts[i].slice) + "/" + to_string(opts[i].switchTime);
if(opts[i].alg==RRP) scheduler += "-" + to_string(opts[i].slice) + "/" + to_string(opts[i].prioritySlice) + "/" + to_string(opts[i].switchTime);
/* determine if the text is to long for the dynamic column */
if(scheduler.length()+2>ww)
{
toLong=true;
break;
}
/**/
ss << fixed << setprecision(2) << setw(ww) << scheduler << setw(w) << avgTurnAround << setw(w) << avgWaiting << cpuUtilization << endl;
}
/**/
ww++; //increase the width of the dynamic column
} while(toLong);
/* output to the console */
string output;
while(!ss.eof())
{
string temp;
getline(ss, temp);
output += (temp+"\n");
}
cout << output;
/**/
}
/* Function: readInProcesses
* Usage: vector<process> ps;
readInProcess("P.dat", ps);
* -------------------------------------------
* Saves the data in a formatted file (eg. "P.dat") into a vector of processes (eg. ps).
* Each line of the file must contain two numbers separated by a space:
* - The first number is the arrival time (in milliseconds),
* - The second number is the amount of time the process requires to complete (in milliseconds)
* eg. "30 2000"
*/
void readInProcesses(string filename, vector<process> & ps)
{
ps.clear();
ifstream p(filename, fstream::in);
string line;
while(p.good())
{
getline(p, line);
if(line.length()>0 && isprint(line[0]))
{
int end = 0; //holds the current position in the line
process pr; //holds the input
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a number doesnt come next, error */
if(!isdigit(line[end]))
{
cerr << "ERROR-- readInProcesses: " << filename << " '"<< line << "' - Each line MUST contain two positive numbers separated by a single space." << endl;
exit(EXIT_FAILURE);
}
/**/
/* save the arrival time */
for(; end<line.length() && isdigit(line[end]); end++) {}
pr.arrival = stoi(line.substr(0,end));
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a number doesnt come next, error */
if(!isdigit(line[end]))
{
cerr << "ERROR-- readInProcesses: " << filename << " '"<< line << "' - Each line MUST contain two positive numbers separated by a single space." << endl;
exit(EXIT_FAILURE);
}
/**/
/* save the burst time */
int firstDigit = end;
for(; end<line.length() && isdigit(line[end]); end++) {}
pr.burst = stoi(line.substr(firstDigit, end-firstDigit+1));
if(pr.burst==0) //error if the burst is 0
{
cerr << "ERROR-- readInProcesses: " << filename << " '"<< line << "' - Ensure that all process burst times are > 0." << endl;
exit(EXIT_FAILURE);
}
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a something comes next, error */
if(end<line.length() && isprint(line[end]))
{
cerr << "ERROR-- readInProcesses: " << filename << " '"<< line << "' - Each line MUST only contain two numbers separated by a single space. No lagging spaces." << endl;
exit(EXIT_FAILURE);
}
/**/
addProcessByArrival(pr, ps);
}
}
p.close();
}
/* Function: readInOptions
* Usage: vector<option> opts;
readInOptions("S.dat", opts);
* -------------------------------------------
* Saves the data from a properly formatted file (eg. "S.dat") into a vector of cpu scheduling options (eg. opts).
* Each line of the file must contain the algorithm identifier, followed by an optional integer pair lead with a dash:
* - The algorithm identifier must be one of the following: FCFS, PSJF, NPSJF, RR, RRP
* - If RR, the integer pair represents the Time Slice (S) and the Context Switching Time (T). (eg. S/T)
* - If RRP, the integer pair represents the Time Slice (S), the Priority Time Slice (PS)
and the Context Switching Time (T). (eg. S/PS/T)
* eg. "FCFS" "RR-100/10" "RRP-100/1000/10"
*/
void readInOptions(string filename, vector<option> & opts)
{
opts.clear();
ifstream s(filename, fstream::in);
string line;
while(s.good())
{
getline(s, line);
if(line.length()>0 && isprint(line[0]))
{
int end = 0; //holds the current position in the line
option opt; //holds the input
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a letter doesnt come next, error */
if(!isalpha(line[end]))
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - Each line MUST start with a letter." << endl;
exit(EXIT_FAILURE);
}
/**/
/* save the option */
int start = end;
for(; end<line.length() && isalpha(line[end]); end++) {}
string prospect = line.substr(start,end-start);
for(int i=0; i<NUM_ALGORITHMS; i++)
{
if(prospect==ALGORITHM[i])
{
opt.alg = (algorithm)i;
break;
}
else if(i==NUM_ALGORITHMS-1)
{
cerr << "ERROR-- readInOptions: " << filename << " - '"<< prospect << "' is not supported." << endl;
exit(EXIT_FAILURE);
}
}
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if RR or RRP and a dash comes next, read in the integer pair*/
if((opt.alg==RR || opt.alg==RRP) && end<line.length() && line[end] == '-')
{
end++;
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a number doesnt come next, error */
if(!isdigit(line[end]))
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - Each dash MUST be followed by a number." << endl;
exit(EXIT_FAILURE);
}
/**/
/* save the slice */
int firstDigit = end;
for(; end<line.length() && isdigit(line[end]); end++) {}
opt.slice = stoi(line.substr(firstDigit, end-firstDigit+1));
if(opt.slice==0) // error if the slice is 0
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - Ensure that all RR and RRP time slices are > 0." << endl;
exit(EXIT_FAILURE);
}
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a slash and a number dont come next, error */
int slashLoc = end++;
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
if(line[slashLoc]!='/' || !isdigit(line[end]))
{
if(opt.alg==RR) cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - For RR, there MUST be two numbers each separated by a slash ( TimeSlice/ContextSwitchTime )" << endl;
else if(opt.alg==RRP) cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - For RRP, there MUST be three numbers each separated by a slash ( TimeSlice/PriorityTimeSlice/ContextSwitchTime )" << endl;
exit(EXIT_FAILURE);
}
if(opt.alg==RR)
{
/**/
/* save the switch time */
firstDigit = end;
for(; end<line.length() && isdigit(line[end]); end++) {}
opt.switchTime = stoi(line.substr(firstDigit, end-firstDigit+1));
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a something comes next, error */
if(end<line.length() && isprint(line[end]))
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - For RR, there MUST only be two numbers each separated by a slash ( TimeSlice/ContextSwitchTime )" << endl;
exit(EXIT_FAILURE);
}
/**/
}
else if(opt.alg==RRP)
{
/**/
/* save the priority slice */
firstDigit = end;
for(; end<line.length() && isdigit(line[end]); end++) {}
opt.prioritySlice = stoi(line.substr(firstDigit, end-firstDigit+1));
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a slash and a number dont come next, error */
slashLoc = end++;
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
if(line[slashLoc]!='/' || !isdigit(line[end]))
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - For RRP, there MUST be three numbers each separated by a slash ( TimeSlice/PriorityTimeSlice/ContextSwitchTime )" << endl;
exit(EXIT_FAILURE);
}
/**/
/* save the switch time */
firstDigit = end;
for(; end<line.length() && isdigit(line[end]); end++) {}
opt.switchTime = stoi(line.substr(firstDigit, end-firstDigit+1));
/**/
/* skip whitespace */
while(isblank(line[end])) end++;
/**/
/* if a something comes next, error */
if(end<line.length() && isprint(line[end]))
{
cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - For RRP, there MUST only be three numbers each separated by a slash ( TimeSlice/PriorityTimeSlice/ContextSwitchTime )" << endl;
exit(EXIT_FAILURE);
}
/**/
}
}
/* if nothing come next, save the default integer pair*/
else if(opt.alg!=RR && opt.alg!=RRP && (end>=line.length() || !isprint(line[end])))
{
opt.slice = 0;
opt.switchTime = 0;
}
/**/
else
{
if(line[end]=='-') cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - Only RR and RRP support '-' modifiers." << endl;
else if(opt.alg==RR || opt.alg==RRP) cerr << "ERROR-- readInOptions: " << filename << " '"<< line << "' - RR and RRP must be followed by a dash and the appropriate modifiers (eg. RR-TimeSlice/ContextSwitchTime , RRP-TimeSlice/PriorityTimeSlice/ContextSwitchTime)." << endl;
else cerr << "ERROR-- readInOptions: S.dat '"<< line << "' - Ensure that each line ONLY contains a single valid entry." << endl;
exit(EXIT_FAILURE);
}
opts.push_back(opt);
}
}
s.close();
}