-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR.cpp
More file actions
92 lines (82 loc) · 2.33 KB
/
RR.cpp
File metadata and controls
92 lines (82 loc) · 2.33 KB
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
/*
AUTHORS: Dante Monaldo
FILENAME: RR.cpp
DESCRIPTION: Implementation of functions declared in RR.h
*/
#include "RR.h"
// Constructor
RR::RR(string input, int quantum) : Scheduler(input) {
timeQuantum = quantum;
parseInputFile();
}
// Run the RR scheduler
void RR::runScheduler() {
bool allProcessesComplete = false;
while (!allProcessesComplete) {
for (unsigned i = 0; i < process.size(); i++) {
// Run process if arrival time is after current CPU time
// and if the process has not been completed yet
if (process[i].arrival <= timeCounter && process[i].state) {
for (int j = 0; j < timeQuantum; j++) {
timeCounter++;
burstCounter++;
process[i].timeRemaining--;
printRunProcess(process[i].pid);
// Mark process as complete
if (process[i].timeRemaining <= 0) {
printCompleteProcess(process[i].pid);
process[i].completionTime = timeCounter;
process[i].state = false;
break;
}
}
}
}
// Check if all processes have completed or are idle
bool completed = true;
bool idle = false;
for (unsigned i = 0; i < process.size(); i++) {
if (process[i].state) {
completed = false;
}
if (process[i].arrival > timeCounter) {
idle = true;
}
}
// If all processes have completed, exit while loop
if (completed) {
allProcessesComplete = true;
} else if (idle) {
// Increment time if there are no processes to run and the CPU is idle
timeCounter++;
}
}
}
// Returns the average response time
double RR::avgRespQuery() {
double sum = 0;
for (int i = 0; i < pidCount; i++) {
sum += i * timeQuantum;
}
return sum / pidCount;
}
// Returns the average wait time
double RR::avgWaitQuery() {
double sum = 0;
for (int i = 0; i < pidCount; i++) {
sum += process[i].completionTime - process[i].arrival - process[i].burst;
}
return sum / pidCount;
}
// Returns the average turnaround time
double RR::avgTurnaroundQuery() {
double sum = 0;
for (int i = 0; i < pidCount; i++) {
sum += process[i].completionTime - process[i].arrival;
}
return sum / pidCount;
}
// Returns the CPU usage in percentage form
double RR::cpuUseQuery() {
return ((double)burstCounter / (double)timeCounter) * 100;
}