-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4ROUND_ROBIN.cxx
182 lines (147 loc) · 2.64 KB
/
4ROUND_ROBIN.cxx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<iostream>
#include<queue>
using namespace std;
struct process
{
int id;
int bt; //to store the burst time
int bti; //to store the initial burst time
int at;
int wt;
int c;
int tat;
int rt;
int flag;
};
int tq;
queue<struct process *>q;
int ct=0;
void showdata(struct process * arry[],int n)
{
struct process *p;
cout<<"\nPro\t"<<"Arr\t"<<"Bur\tRes\tCom\tTurn\tWait\n";
for(int i=0;i<n;i++)
{
p=arry[i];
cout<<p->id<<"\t"<<p->at<<"\t"<<p->bti<<"\t"<<p->rt<<"\t"<<p->c<<"\t"<<p->tat<<"\t"<<p->wt<<"\n";
}
}
void check_arrival(struct process *a[],int n)
{
struct process *p;
for(int i=0;i<n;i++)
{
p=a[i];
if((p->flag==0) && (p->at<=ct ))
{
p->flag=3;//so that process cant be repushed
q.push(p);
}
}
}
void round_robin(struct process * a[],int n)
{
struct process *p;
struct process *min_at_array[20];
struct process *exec;
int min_at;
p=a[0];
min_at=p->at;
exec=p;
for(int i=0;i<n;i++)
{
p=a[i];
if(p->at<min_at)
exec=p;
}
q.push(exec);
while(!q.empty())
{
exec=q.front();
cout<<"->"<<exec->id;
if(exec->flag==0 || exec->flag==3)
{
exec->rt=ct-exec->at;
exec->flag=2;//Process responded but remaining executing fully
}
if(tq<exec->bt)
{
ct=ct+tq;
exec->bt=exec->bt-tq;
exec->flag=2;
check_arrival(a,n);
q.push(exec);
}
else
{
if((tq==exec->bt) || (tq>exec->bt))
{
ct=ct+exec->bt;
exec->c=ct;
exec->bt=exec->bt-tq;
exec->flag=1;
check_arrival(a,n);
}
}
q.pop();
}
}
void update_data(struct process * a[],int n)
{
struct process * p;
for(int i=0;i<n;i++)
{
p=a[i];
p->tat=p->c-p->at;
p->wt=p->tat-p->bti;
}
}
void awt_atat_art(struct process *a[],int n)
{
struct process *p;
float awt=0,atat=0,art=0;
for(int i=0;i<n;i++)
{
p=a[i];
awt=awt+p->wt;
atat=atat+p->tat;
art=art+p->rt;
}
awt=awt/n;
atat=atat/n;
art=art/n;
cout<<"\nAverage Response Time is "<<art<<"\nAverage Turn Around Time is "<<atat<<"\nAverage Waiting Time is "<<awt;
}
int main()
{
int n;
cout<<"Enter the time quantum ";
cin>>tq;
cout<<"Enter number of process ";
cin>>n;
struct process * array[20];
for(int i=0;i<n;i++)
{
int b,a,pr,ct=0;
struct process *p;
p=(struct process *) malloc(sizeof(struct process));
array[i]=p;
p->id=i+1;
cout<<"\nEnter arrival time and burst time for process"<<i+1<<" ";
cin>>a;
cin>>b;
p->bt=b;
p->bti=b;
p->at=a;
p->flag=0;
p->wt=0;
p->tat=0;
p->rt=0;
p->c=ct;
}
cout<<"\nOrder of execution is ";
round_robin(array,n);
update_data(array,n);
showdata(array,n);
awt_atat_art(array,n);
}