-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_robin.cpp
126 lines (118 loc) · 3.38 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
120
121
122
123
124
125
126
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
// Practical 7 round robin
struct Process{
int pid;
int burst_time;
int arrival_time;
int complition_time;
int response_time;
int waiting_time;
int turnAround_time;
int starting_time;
};
class RR
{
private:
Process *p;
int *remaining_time;
int num,quantum,t;
float wt,tt;
public:
RR();
void input();
void calculate();
void display();
};
RR::RR()
{
cout<<"Enter the number of process : ";
cin>>num;
cout<<"Enter the time Quantum value : ";
cin>>quantum;
p = new Process[num];
remaining_time = new int[num];
}
void RR::input()
{
cout<<endl<<"Enter the respective process ids "<<endl;
for(int i=0;i<num;i++)
{
cout<<"P"<<" : ";
cin>>p[i].pid;
}
cout<<"Enter the Burst time for processes : "<<endl;
for(int i=0;i<num;i++)
{
p[i].arrival_time=0;
cout<<"P"<<p[i].pid<<" : ";
cin>>p[i].burst_time;
remaining_time[i] = p[i].burst_time;
}
}
void RR::calculate()
{
int c=0;
t=0;
cout<<setw(40)<<"---------------GANTT CHART---------------"<<endl<<endl<<endl;
cout<<p[0].arrival_time;
while(c!=num)
{
for(int i = 0 ;i <num;i++)
{
if(i==0)
p[i].starting_time=p[i].arrival_time;
else
p[i].starting_time= max(p[i].arrival_time,p[i-1].complition_time);
if(remaining_time[i] > 0 )
{
if(remaining_time[i] > quantum)
{
t = t + quantum;
remaining_time[i] = remaining_time[i] - quantum;
cout<<"|---P"<<p[i].pid<<"---|";
p[i].complition_time=t;
cout<<t;
}
else
{
t = t + remaining_time[i];
p[i].complition_time=t;
p[i].turnAround_time=p[i].complition_time-p[i].arrival_time;
p[i].waiting_time = p[i].turnAround_time - p[i].burst_time;
p[i].response_time=p[i].starting_time-p[i].arrival_time;
remaining_time[i] = 0;
cout<<"|---P"<<p[i].pid<<"---|";
cout<<t;
c++;
}
}
}
}
cout<<endl<<endl;
}
void RR::display()
{
cout<<endl<<"Processes | "<<"Burst time | "<<"Complition time | "<<"Waiting time | "<<"Response time | "<<" Turn around time "<<endl;
for(int i=0;i<num;i++)
{
wt=wt+p[i].waiting_time;
tt=tt+p[i].turnAround_time;
cout<<"P"<<p[i].pid<<setw(15)<<p[i].burst_time<<setw(15)<<p[i].complition_time<<setw(15)<<p[i].waiting_time<<setw(15)<<p[i].response_time<<setw(15)<<p[i].turnAround_time<<endl;
}
cout<<endl<<" Total waiting time : "<<wt<<endl;
cout<<" Average waiting time : "<<(wt/num)<<endl;
cout<<" Total turnaround time : "<<tt<<endl;
cout<<" Average turnaround time : "<<(tt/num)<<endl;
}
int main()
{
RR obj;
obj.input();
obj.calculate();
obj.display();
getch();
return 0;
}