-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobinAlgo.cpp
More file actions
137 lines (102 loc) · 3.03 KB
/
Copy pathRoundRobinAlgo.cpp
File metadata and controls
137 lines (102 loc) · 3.03 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Name : Annie Bhalla
Roll No. : 19HCS4009
Course : BSC (H) Computer Science
Semester : 3
Subject : Operating System
Title : Round Robin Scheudling ( always Preemptive)
*/
#include<iostream>
using namespace std;
int time_quantum;
struct RRobin
{
int id;
int arrivalTime=0;
int burstTime;
int turnaroundTime;
int finishTime;
int waitingTime;
bool status =0;
int remaining_bt;
};
// (1) to enter the details of all processes
void input(int len, RRobin obj[])
{
for(int i=0;i<len;i++)
{
cout<<"\n Enter Burst Time "<<i+1<<" : "<<endl;
cin>>obj[i].burstTime;
obj[i].id=i+1;
obj[i].remaining_bt = obj[i].burstTime;
}
}
// (2) Displaying process no, arrival time , burst time , finishing time , TA , WT
void display(int n, RRobin process[])
{
cout<<" Process No. "<<'\t'<<"Arrival Time"<<'\t'<<"Burst Time"<<'\t'<<" Finishing Time"<<'\t'<<" Turn Around Time"<<'\t'<<"Waiting Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<process[i].id<<'\t'<<'\t'<<process[i].arrivalTime<<'\t'<<'\t'<<'\t'<<process[i].burstTime<<'\t'<<'\t'<<process[i].finishTime<<'\t'<<'\t'<<process[i].turnaroundTime<<'\t'<<'\t'<<process[i].waitingTime<<endl;
}
}
// scheduling algo
void schedule(int nop , RRobin p[])
{
int completed=0, timer=0;
while( completed!= nop)
{
for( int i=0;i<nop;i++)
{
if(p[i].status==0)
{
if( p[i].remaining_bt <= time_quantum )
{
timer += p[i].remaining_bt;
p[i].remaining_bt =0;
p[i].status= 1;
completed++;
p[i].finishTime = timer;
p[i].turnaroundTime= p[i].finishTime;
p[i].waitingTime = p[i].turnaroundTime - p[i].burstTime;
}
else
if( p[i].remaining_bt> time_quantum)
{
timer += time_quantum;
p[i].remaining_bt -= time_quantum;
}
}
}
}
}
// driver code
int main()
{
cout<<"\n Enter the time quantum : ";
cin>>time_quantum;
int nop; // nop= number of processes
RRobin *process;
cout<<"\n Enter the number of Processes : ";
cin>>nop;
process = new RRobin[nop];
input(nop,process); // calling input function to enter AT and BT and priority
// Displaying AT and BT of processes
cout<<" Process No. "<<'\t'<<"Arrival Time"<<'\t'<<"Burst Time"<<endl;
for(int i=0;i<nop;i++)
{
cout<<process[i].id<<'\t'<<'\t'<<process[i].arrivalTime<<'\t'<<'\t'<<process[i].burstTime<<endl;
}
schedule(nop, process); // calling schedule function
cout<<endl<<endl<<endl;
float avg_waiting_time=0;
for(int i=0;i<nop;i++)
avg_waiting_time += process[i].waitingTime;
avg_waiting_time= avg_waiting_time / nop;
cout<<endl<<endl<<endl;
cout<<" ******* Result ********"<<endl<<endl;
display(nop,process);
cout<<endl<<endl;
cout<<"\n Average waiting Time is : "<<avg_waiting_time<<" ms ";
delete [] process;
return 0;
}