-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR.cpp
More file actions
130 lines (107 loc) · 3.82 KB
/
RR.cpp
File metadata and controls
130 lines (107 loc) · 3.82 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
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
#include "RR.h"
bool compare1(process *p1, process *p2)
{
return p1->arrival_time < p2->arrival_time;
}
bool compare2(process p1, process p2)
{
return p1.pid < p2.pid;
}
void startTimeCalculation(process *p, float current_time)
{
p->start_time = max(p->arrival_time, current_time);
}
void completionTime(process *p, float current_time)
{
p->completion_time = current_time;
}
void rr(vector<process *> *processes, int time_quantum, float context_switch)
{
// index to trace processes
unsigned int index;
// tracing rarmaining burst time for a process
vector<float> remaining_burst(processes->size(), 0);
// filling the arrray with full burst time corresponding to processes in processes vector
for (unsigned int i = 0; i < processes->size(); i++)
remaining_burst[i] = processes->at(i)->burst_time;
// init queue
queue<int> q;
// push first start time
q.push(0);
// this is to be used for indecating the current time/start time for a process when it moves from ready to running
float current_time = 0;
// this is for checking the completness of processes
vector<bool> is_completed(processes->size(), false);
is_completed[0] = true;
// counting the cpu time for processes that is processed plus the overhead time (context switch)
float timer = 0;
while (!q.empty())
{
// poping front of queue and use it as index
index = q.front();
q.pop();
// calculate start time if current process is the queue front
if (remaining_burst[index] == processes->at(index)->burst_time)
{
startTimeCalculation(processes->at(index), current_time);
current_time = processes->at(index)->start_time;
}
// doing the process if still isn't reached 0 burst time
if (remaining_burst[index] - time_quantum > 0)
{
// substracting quantum time from burst time
remaining_burst[index] -= time_quantum;
// add quontum time and context switch over load to timer
timer += time_quantum + context_switch;
current_time += time_quantum;
}
else
{
// add remaining cpu burst time + overhead time
timer += remaining_burst[index] + context_switch;
current_time += remaining_burst[index];
remaining_burst[index] = 0;
// do calculation for process
completionTime(processes->at(index), current_time);
turnAround(processes->at(index));
waitingTime(processes->at(index));
responseTime(processes->at(index));
}
for (unsigned int i = 1; i < processes->size(); i++)
{
// check completness
if (remaining_burst[i] > 0 && processes->at(i)->arrival_time <= current_time && is_completed[i] == false)
{
q.push(i);
is_completed[i] = true;
}
}
// if not finished
if (remaining_burst[index] > 0)
q.push(index);
// if queue is empty but there is an unfinished process
if (q.empty())
{
for (unsigned int i = 1; i < processes->size(); i++)
{
if (remaining_burst[i] > 0)
{
q.push(i);
is_completed[i] = true;
break;
}
}
}
}
/*
printing all neccessery information
*/
cout << "PID "
<< " Waiting time "
<< " Turn around time"
<< endl;
for (unsigned int i = 0; i < processes->size(); i++)
cout << " " << processes->at(i)->pid << "\t" << processes->at(i)->waiting_time << "\t\t" << processes->at(i)->turnaround_time << endl;
cout << "\nCPU clock time = "
<< timer << endl;
}