@@ -24,6 +24,12 @@ bool descendingly_by_response_ratio(tuple<string, double, int> a, tuple<string,
24
24
return get<1 >(a) > get<1 >(b);
25
25
}
26
26
27
+ bool byPriorityLevel (const tuple<int ,int ,int >&a,const tuple<int ,int ,int >&b){
28
+ if (get<0 >(a)==get<0 >(b))
29
+ return get<2 >(a)> get<2 >(b);
30
+ return get<0 >(a) > get<0 >(b);
31
+ }
32
+
27
33
void clear_timeline ()
28
34
{
29
35
for (int i=0 ; i<last_instant; i++)
@@ -46,6 +52,11 @@ int getServiceTime(tuple<string, int, int> &a)
46
52
return get<2 >(a);
47
53
}
48
54
55
+ int getPriorityLevel (tuple<string, int , int > &a)
56
+ {
57
+ return get<2 >(a);
58
+ }
59
+
49
60
50
61
void firstComeFirstServe ()
51
62
{
@@ -389,9 +400,52 @@ void feedbackQ2i()
389
400
390
401
}
391
402
392
- void aging ()
403
+ void aging (int originalQuantum )
393
404
{
405
+ vector<tuple<int ,int ,int >>v; // pair of priority level and index
406
+ int j=0 ;
407
+ int currentProcess=-1 ;
408
+ for (int time =0 ;time<last_instant;time++){
409
+ while (j<process_count && getArrivalTime (processes[j])<=time){
410
+ v.push_back (make_tuple (getPriorityLevel (processes[j]),j,0 ));
411
+ j++;
412
+ }
413
+
414
+ for (int i=0 ;i<v.size ();i++){
415
+ if (get<1 >(v[i])==currentProcess){
416
+ get<2 >(v[i])=0 ;
417
+ get<0 >(v[i])=getPriorityLevel (processes[currentProcess]);
418
+ }
419
+ else {
420
+ get<0 >(v[i])++;
421
+ get<2 >(v[i])++;
422
+ }
423
+
424
+ }
425
+ sort (v.begin (),v.end (),byPriorityLevel);
426
+ currentProcess=get<1 >(v[0 ]);
427
+ timeline[time][currentProcess]=' *' ;
428
+ }
429
+
430
+
431
+ for (int i = 0 ; i < process_count; i++)
432
+ {
433
+ int arrivalTime = getArrivalTime (processes[i]);
434
+ for (int k = arrivalTime; k < last_instant; k++)
435
+ {
436
+ if (timeline[k][i] != ' *' )
437
+ timeline[k][i] = ' .' ;
438
+ }
439
+ }
440
+ }
394
441
442
+ void printAlgorithm (int algorithm_index)
443
+ {
444
+ int algorithm_id = algorithms[algorithm_index].first - ' 0' ;
445
+ if (algorithm_id==2 )
446
+ cout << ALGORITHMS[algorithm_id] <<algorithms[algorithm_index].second <<endl;
447
+ else
448
+ cout << ALGORITHMS[algorithm_id] << endl;
395
449
}
396
450
397
451
void printProcesses ()
@@ -457,7 +511,7 @@ void printNormTurn()
457
511
}
458
512
void printStats (int algorithm_index)
459
513
{
460
- cout<<endl ;
514
+ printAlgorithm (algorithm_index) ;
461
515
printProcesses ();
462
516
printArrivalTime ();
463
517
printServiceTime ();
@@ -484,40 +538,41 @@ void printTimeline(int algorithm_index)
484
538
cout << " ------------------------------------------------\n " ;
485
539
}
486
540
487
- void execute_algorithm (char algorithm_id, int quantum)
541
+ void execute_algorithm (char algorithm_id, int quantum,string operation )
488
542
{
489
543
switch (algorithm_id)
490
544
{
491
545
case ' 1' :
492
- cout<<" FCFS " ;
546
+ if (operation==TRACE) cout<<" FCFS " ;
493
547
firstComeFirstServe ();
494
548
break ;
495
549
case ' 2' :
496
- cout<<" RR-" <<quantum<<" " ;
550
+ if (operation==TRACE) cout<<" RR-" <<quantum<<" " ;
497
551
roundRobin (quantum);
498
552
break ;
499
553
case ' 3' :
500
- cout<<" SPN " ;
554
+ if (operation==TRACE) cout<<" SPN " ;
501
555
shortestProcessNext ();
502
556
break ;
503
557
case ' 4' :
504
- cout<<" SRT " ;
558
+ if (operation==TRACE) cout<<" SRT " ;
505
559
shortestRemainingTime ();
506
560
break ;
507
561
case ' 5' :
508
- cout<<" HRRN " ;
562
+ if (operation==TRACE) cout<<" HRRN " ;
509
563
highestResponseRatioNext ();
510
564
break ;
511
565
case ' 6' :
512
- cout<<" FB-1 " ;
566
+ if (operation==TRACE) cout<<" FB-1 " ;
513
567
feedbackQ1 ();
514
568
break ;
515
569
case ' 7' :
516
- cout<<" FB-2i " ;
570
+ if (operation==TRACE) cout<<" FB-2i " ;
517
571
feedbackQ2i ();
518
572
break ;
519
573
case ' 8' :
520
- aging ();
574
+ if (operation==TRACE)cout<<" Aging " ;
575
+ aging (quantum);
521
576
break ;
522
577
default :
523
578
break ;
@@ -532,7 +587,7 @@ int main()
532
587
for (int idx = 0 ; idx < (int )algorithms.size (); idx++)
533
588
{
534
589
clear_timeline ();
535
- execute_algorithm (algorithms[idx].first , algorithms[idx].second );
590
+ execute_algorithm (algorithms[idx].first , algorithms[idx].second ,operation );
536
591
if (operation == TRACE)
537
592
printTimeline (idx);
538
593
else if (operation == SHOW_STATISTICS)
0 commit comments