-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobin.cpp
66 lines (55 loc) · 1.54 KB
/
RoundRobin.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
#include <iostream>
using namespace std;
int main() {
float n, quantum, avwt = 0, avtat = 0;
float pid[50], bt[50], at[50], rem_time[50], comp_time[50];
float wt[50], tat[50];
cout << "Enter total number of processes: ";
cin >> n;
cout << "Enter time quantum: ";
cin >> quantum;
cout << "\nEnter the arrival time and burst time for each process: " << endl;
for (int i = 0; i < n; i++) {
cout << "Process " << i + 1 << ":\n";
pid[i] = i + 1;
cout << "\tArrival time: ";
cin >> at[i];
cout << "\tBurst time: ";
cin >> bt[i];
rem_time[i] = bt[i];
wt[i] = tat[i] = 0;
}
int curr_time = 0, total_rem_time = n;
while (total_rem_time > 0) {
for (int i = 0; i < n; i++) {
if (rem_time[i] > 0) {
if (rem_time[i] <= quantum) {
curr_time += rem_time[i];
rem_time[i] = 0;
total_rem_time--;
comp_time[i] = curr_time;
} else {
curr_time += quantum;
rem_time[i] -= quantum;
}
}
}
}
for (int i = 0; i < n; i++) {
tat[i] = comp_time[i] - at[i];
wt[i] = tat[i] - bt[i];
avwt += wt[i];
avtat += tat[i];
}
avwt /= n;
avtat /= n;
cout
<< "\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";
for (int i = 0; i < n; i++) {
cout << "P" << pid[i] << "\t\t" << at[i] << "\t\t\t\t" << bt[i] << "\t\t\t"
<< wt[i] << "\t\t\t\t" << tat[i] << endl;
}
cout << "\nAverage Waiting Time: " << avwt;
cout << "\nAverage Turnaround Time: " << avtat;
return 0;
}