-
Notifications
You must be signed in to change notification settings - Fork 0
/
round-robin.cpp
119 lines (106 loc) · 3.31 KB
/
round-robin.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct process
{
string id;
int burst, waiting, turnaround;
};
int main()
{
int size;
cin >> size;
vector<process> x(size);
queue<process> waiting_list;
for (int i = 0; i < size; i++)
{
cin >> x[i].id >> x[i].burst;
waiting_list.push(x[i]);
}
int time_now = 0, quantum = 4;
double avg_waiting = 0, avg_turnaround = 0;
vector<pair<string, int>> timeline;
while (!waiting_list.empty())
{
process now = waiting_list.front();
waiting_list.pop();
int quantum_now = min(now.burst, quantum);
if (waiting_list.size() == 0)
{
quantum_now = now.burst;
}
time_now += quantum_now;
timeline.push_back({now.id, time_now});
now.burst -= quantum_now;
if (!now.burst)
{
for (auto &p: x)
{
if (p.id == now.id)
{
p.turnaround = time_now;
p.waiting = time_now - p.burst;
avg_waiting += p.waiting;
avg_turnaround += p.turnaround;
break;
}
}
}
else waiting_list.push(now);
}
string gantt_chart = "|", border = "-";
for (auto z: timeline)
{
gantt_chart += " " + z.first + " |";
border += "-------";
}
cout << "Gantt Chart:\n" << border << "\n"
<< gantt_chart << "\n"
<< border << "\n"
<< "0";
int index = 0;
for (int i = 1; gantt_chart[i]; i++)
{
if (gantt_chart[i] == '|')
{
cout << timeline[index].second;
if (timeline[index++].second >= 10)
{
i++;
}
}
else cout << " ";
}
cout << "\n\n"
<< "Average waiting time = " << avg_waiting / size << "\n"
<< "Average turnaround time = " << avg_turnaround / size << "\n\n"
<< "Process | Waiting Time | Turnaround Time\n"
<< "------------------------------------------------\n";
for (int i = 0; i < size; i++)
{
cout << x[i].id << " "
<< x[i].waiting << " "
<< x[i].turnaround << "\n";
}
}
/*//... Sample Input-Output:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
3
P1 24
P2 3
P3 3
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
Gantt Chart:
---------------------------------------------------------
| P1 | P2 | P3 | P1 | P1 | P1 | P1 | P1 |
---------------------------------------------------------
0 4 7 10 14 18 22 26 30
Average waiting time = 5.66667
Average turnaround time = 15.6667
Process | Waiting Time | Turnaround Time
------------------------------------------------
P1 6 30
P2 4 7
P3 7 10
*///...