Repository navigation
Expand file tree
/
Copy pathroundRobinWithoutArrivalTime.cpp
More file actions
56 lines (49 loc) · 1.75 KB
/
Copy pathroundRobinWithoutArrivalTime.cpp
File metadata and controls
56 lines (49 loc) · 1.75 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> burstTimes{4, 1, 8, 1};
vector<int> originalBurstTimes = burstTimes;
int n = burstTimes.size();
vector<int> completionTimes(n, -1);
queue<int> readyQueueIndex;
int timeQuantum = 1;
vector<int> turnAroundTime;
vector<int> waitingTime;
int totalTime = accumulate(burstTimes.begin(), burstTimes.end(), 0);
int timeElapsed = 0;
//filling the ready queue:
for (int i = 0; i < n; i++)
readyQueueIndex.push(i);
while (timeElapsed < totalTime)
{
int indexOfCurrentProcess = readyQueueIndex.front();
if (burstTimes[indexOfCurrentProcess] <= timeQuantum)
{
timeElapsed += burstTimes[indexOfCurrentProcess];
burstTimes[indexOfCurrentProcess] = 0;
completionTimes[indexOfCurrentProcess] = timeElapsed;
}
else
{
timeElapsed += timeQuantum;
burstTimes[indexOfCurrentProcess] -= timeQuantum;
readyQueueIndex.push(indexOfCurrentProcess);
}
readyQueueIndex.pop();
}
//turn around time = completion time (when not considering any specific arrival times)
turnAroundTime = completionTimes;
//computing waiting times:
for (int i = 0; i < n; i++)
waitingTime.push_back(completionTimes[i] - originalBurstTimes[i]);
cout << "\nProccess No. \t\tBurst Time\t\tTurn Around Time\t\tWaiting Time";
for (int i = 0; i < n; i++)
cout << "\nP" << (i + 1) << "\t\t\t"
<< originalBurstTimes[i] << "\t\t\t"
<< turnAroundTime[i] << "\t\t\t\t"
<< waitingTime[i];
cout << "\nAverage Turn Around Time = " << accumulate(turnAroundTime.begin(), turnAroundTime.end(), 0) / (float)n;
cout << "\nAverage Waiting Time = " << accumulate(waitingTime.begin(), waitingTime.end(), 0) / (float)n;
return 0;
}