-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRTF.cpp
More file actions
85 lines (74 loc) · 1.83 KB
/
SRTF.cpp
File metadata and controls
85 lines (74 loc) · 1.83 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
/*
AUTHORS: Alex Runciman
FILENAME: SRTF.cpp
DESCRIPTION: Implementation of functions declared in SRTF.h
*/
#include "SRTF.h"
// Run the SRTF scheduler
void SRTF::runScheduler() {
parseInputFile();
vector<int> rt; // Remaining time
for (int i = 0; i < pidCount; i++) {
rt.push_back(process[i].burst);
waitTime.push_back(-INC);
}
int complete = 0, min = MAX;
int shortest = 0, finishTime;
bool check = false;
// Process until all processes gets completed
while (complete != pidCount) {
for (int j = 0; j < pidCount; j++) {
if ((process[j].arrival <= timeCounter) &&
(rt[j] < min) && rt[j] > 0) {
min = rt[j];
shortest = j;
check = true;
}
}
printRunProcess(shortest+1);
if (check == false) {
timeCounter++;
}
rt[shortest]--;
min = rt[shortest];
if (min == 0)
min = MAX;
if (rt[shortest] == 0) {
complete++;
finishTime = timeCounter + 1;
printCompleteProcess(shortest+1);
waitTime[shortest] = finishTime - process[shortest].burst -
process[shortest].arrival;
if(waitTime[shortest] < 0)
waitTime[shortest] = 0;
}
timeCounter++;
}
}
// Returns the average response time
double SRTF::avgRespQuery() {
return 0;
}
// Returns the average wait time
double SRTF::avgWaitQuery() {
for(int i = 0; i < pidCount; i++){
avgWait+= waitTime[i];
}
return avgWait/pidCount;
}
// Returns the average turnaround time
double SRTF::avgTurnaroundQuery() {
for(int i = 0; i < pidCount; i++){
avgTurn = avgTurn + process[i].burst + waitTime[i];
}
return avgTurn/pidCount;
}
// Returns the CPU usage in percentage form
double SRTF::cpuUseQuery() {
double tot = 0;
for (int i = 0; i < pidCount; i++) {
tot += process[i].burst;
}
tot = tot / timeCounter * 100;
return tot;
}