Skip to content

Commit cabdab9

Browse files
committed
Finish Aging Algorithm
1 parent 574739a commit cabdab9

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

main.cpp

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ bool descendingly_by_response_ratio(tuple<string, double, int> a, tuple<string,
2424
return get<1>(a) > get<1>(b);
2525
}
2626

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+
2733
void clear_timeline()
2834
{
2935
for(int i=0; i<last_instant; i++)
@@ -46,6 +52,11 @@ int getServiceTime(tuple<string, int, int> &a)
4652
return get<2>(a);
4753
}
4854

55+
int getPriorityLevel(tuple<string, int, int> &a)
56+
{
57+
return get<2>(a);
58+
}
59+
4960

5061
void firstComeFirstServe()
5162
{
@@ -389,9 +400,52 @@ void feedbackQ2i()
389400

390401
}
391402

392-
void aging()
403+
void aging(int originalQuantum)
393404
{
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+
}
394441

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;
395449
}
396450

397451
void printProcesses()
@@ -457,7 +511,7 @@ void printNormTurn()
457511
}
458512
void printStats(int algorithm_index)
459513
{
460-
cout<<endl;
514+
printAlgorithm(algorithm_index);
461515
printProcesses();
462516
printArrivalTime();
463517
printServiceTime();
@@ -484,40 +538,41 @@ void printTimeline(int algorithm_index)
484538
cout << "------------------------------------------------\n";
485539
}
486540

487-
void execute_algorithm(char algorithm_id, int quantum)
541+
void execute_algorithm(char algorithm_id, int quantum,string operation)
488542
{
489543
switch (algorithm_id)
490544
{
491545
case '1':
492-
cout<<"FCFS ";
546+
if(operation==TRACE)cout<<"FCFS ";
493547
firstComeFirstServe();
494548
break;
495549
case '2':
496-
cout<<"RR-"<<quantum<<" ";
550+
if(operation==TRACE)cout<<"RR-"<<quantum<<" ";
497551
roundRobin(quantum);
498552
break;
499553
case '3':
500-
cout<<"SPN ";
554+
if(operation==TRACE)cout<<"SPN ";
501555
shortestProcessNext();
502556
break;
503557
case '4':
504-
cout<<"SRT ";
558+
if(operation==TRACE)cout<<"SRT ";
505559
shortestRemainingTime();
506560
break;
507561
case '5':
508-
cout<<"HRRN ";
562+
if(operation==TRACE)cout<<"HRRN ";
509563
highestResponseRatioNext();
510564
break;
511565
case '6':
512-
cout<<"FB-1 ";
566+
if(operation==TRACE)cout<<"FB-1 ";
513567
feedbackQ1();
514568
break;
515569
case '7':
516-
cout<<"FB-2i ";
570+
if(operation==TRACE)cout<<"FB-2i ";
517571
feedbackQ2i();
518572
break;
519573
case '8':
520-
aging();
574+
if(operation==TRACE)cout<<"Aging ";
575+
aging(quantum);
521576
break;
522577
default:
523578
break;
@@ -532,7 +587,7 @@ int main()
532587
for (int idx = 0; idx < (int)algorithms.size(); idx++)
533588
{
534589
clear_timeline();
535-
execute_algorithm(algorithms[idx].first, algorithms[idx].second);
590+
execute_algorithm(algorithms[idx].first, algorithms[idx].second,operation);
536591
if (operation == TRACE)
537592
printTimeline(idx);
538593
else if (operation == SHOW_STATISTICS)

0 commit comments

Comments
 (0)