-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRound-Robin-Code.cpp
105 lines (85 loc) · 3.4 KB
/
Round-Robin-Code.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
// Round Robin with Arrival Time
// Mounish Sai (https://github.com/pvsmounish)
#include<iostream>
using namespace std;
class Process{
public:
string processName;
int burstTime;
int arrivalTime;
int waitingTime;
int completionTime;
int responseTime;
int turnAroundTime;
int remainingTime;
void initialize(){
waitingTime = 0;
responseTime = 0;
turnAroundTime = 0;
remainingTime = burstTime;
}
};
int main(){
int numOfProcesses;
int timeQuantum;
int currentTime = 0;
cout << "\nEnter Time Quantum: ";
cin >> timeQuantum;
cout << "\nEnter no. of processes: ";
cin >> numOfProcesses;
Process processes[numOfProcesses];
for(int n=0;n<numOfProcesses;n++){
cout << "Enter Process Name for " << (n+1) << ": ";
cin >> processes[n].processName;
cout << "Enter Arrival Time for Process " << (n+1) << ": ";
cin >> processes[n].arrivalTime;
cout << "Enter Burst Time for Process " << (n+1) << ": ";
cin >> processes[n].burstTime;
processes[n].initialize();
}
cout << "\n" << endl;
currentTime = processes[0].arrivalTime;
int remainingProcesses = numOfProcesses;
for(int i=0;i<numOfProcesses;i=(i+1)%numOfProcesses){
if(processes[i].remainingTime > 0 && processes[i].arrivalTime <= currentTime){
if(processes[i].remainingTime == processes[i].burstTime){
processes[i].responseTime = currentTime;
}
if(processes[i].remainingTime <= timeQuantum){
currentTime += processes[i].remainingTime;
processes[i].completionTime = currentTime;
processes[i].remainingTime = 0;
remainingProcesses--;
}
else{
currentTime += timeQuantum;
processes[i].remainingTime -= timeQuantum;
}
}
if(remainingProcesses == 0){
break;
}
}
int sumResponseTime = 0;
int sumCompletionTime = 0;
int sumWaitingTime = 0;
int sumTurnAroundTime = 0;
for(int n=0;n<numOfProcesses;n++){
cout << "\nProcess " << processes[n].processName << ":\n";
cout << "Response Time: " << processes[n].responseTime << endl;
cout << "Completion Time: " << processes[n].completionTime << endl;
processes[n].turnAroundTime = processes[n].completionTime - processes[n].arrivalTime;
processes[n].waitingTime = processes[n].turnAroundTime - processes[n].burstTime;
cout << "Waiting Time: " << processes[n].waitingTime << endl;
cout << "Turn Around Time: " << processes[n].turnAroundTime << "\n" << endl;
sumResponseTime += processes[n].responseTime;
sumCompletionTime += processes[n].completionTime;
sumWaitingTime += processes[n].waitingTime;
sumTurnAroundTime += processes[n].turnAroundTime;
}
cout << "\n\nAverage Response Time for " << (numOfProcesses) << " Processes: " << (float) sumResponseTime/numOfProcesses;
cout << "\n\nAverage Completion Time for " << (numOfProcesses) << " Processes: " << (float) sumCompletionTime/numOfProcesses;
cout << "\n\nAverage Waiting Time for " << (numOfProcesses) << " Processes: " << (float) sumWaitingTime/numOfProcesses;
cout << "\n\nAverage Turn Around Time for " << (numOfProcesses) << " Processes: " << (float) sumTurnAroundTime/numOfProcesses;
return 0;
}