forked from HemangTheHuman/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroundrobin.cpp
More file actions
91 lines (75 loc) · 2.12 KB
/
roundrobin.cpp
File metadata and controls
91 lines (75 loc) · 2.12 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
include <iostream>
using namespace std;
const int N=100005;
int n,quantum_time;
struct process
{
int id;
int burst_time;
int arrival_time;
int waiting_time;
int finishing_time;
int turn_around_time;
int remaining_time;
};
process P[N];
void RoundRobin()
{
int complete,current_time,change;
double total_waiting_time = 0.0;
double total_turn_around_time = 0.0;
for(int i=0; i<n; i++)
P[i].remaining_time = P[i].burst_time;
complete = 0;
current_time = 0;
while(complete < n)
{
change = 0;
for(int i=0; i<n; i++)
{
if(P[i].arrival_time <= current_time && P[i].remaining_time > 0)
{
if(P[i].remaining_time <= quantum_time)
{
complete++;
current_time += P[i].remaining_time;
P[i].finishing_time = current_time;
P[i].turn_around_time = P[i].finishing_time - P[i].arrival_time;
P[i].waiting_time = P[i].turn_around_time - P[i].burst_time;
total_waiting_time += P[i].waiting_time;
total_turn_around_time += P[i].turn_around_time;
P[i].remaining_time = 0;
}
else
{
current_time += quantum_time;
P[i].remaining_time -= quantum_time;
}
change++;
}
}
if(change == 0)
{
current_time++;
}
}
cout<<fixed<<setprecision(2);
cout<<"Average Waiting Time: "<<(total_waiting_time/n)<<"\n";
cout<<"Average Turn Around Time: "<<(total_turn_around_time/n)<<"\n";
return;
}
int main()
{
cout<<"Number of Processes: ";
cin>>n;
cout<<"Quantum time: ";
cin>>quantum_time;
cout<<"Process Ids:\n";
for(int i=0; i<n; i++) cin>>P[i].id;
cout<<"Process Burst Times:\n";
for(int i=0; i<n; i++) cin>>P[i].burst_time;
cout<<"Process Arrival Times:\n";
for(int i=0; i<n; i++) cin>>P[i].arrival_time;
RoundRobin();
return 0;
}