-
Notifications
You must be signed in to change notification settings - Fork 4
/
OSPROJECT.CPP
278 lines (245 loc) · 4.48 KB
/
OSPROJECT.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include<iostream>
using namespace std;
struct process
{
int pno;
int at;
int bt;
int wt;
int tat;
int ct;
int rt;
};
//Global variables
int n;
struct process p[100];
void input()
{
cout<<"Enter the number of processes : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter process number ,Arrival time and burst time : \n";
cin>>p[i].pno>>p[i].at>>p[i].bt;
p[i].rt = p[i].bt;
p[i].wt = 0;
p[i].ct = p[i].at;
}
}
void classical_rr()
{
//Local variables
int i,j,flag;
struct process q[100],r[100];
int rbeg = 0;
int rend = 0;
float l,h;
l=h=0;
int count = 0;
int time = 0;
int tq = 25;
int time_exec ;
float awt = 0,att = 0;
//Copying to another array
for(i=0;i<n;i++)
{
q[i] = p[i];
}
flag = 1;
while(count!=n)
{
//Appends the processes to the ready queue-->In the beginning
if(flag == 1)
{
for(i=0;i<n;i++)
{
if(q[i].at>=l && q[i].at<=h)
{
r[rend] = q[i];
rend++;
}
}
if(rend!=0)
{
flag --;
}
else
{
l=h;
h++;
continue;
}
}
//Execute the process present at rbeg
cout<<"<"<<time<<">"<<"---"<<r[rbeg].pno<<"---";
//Calculating parameters
time_exec = (r[rbeg].rt>tq)?tq:r[rbeg].rt;
r[rbeg].rt -=time_exec;
r[rbeg].wt += time - r[rbeg].ct;
time +=time_exec;
r[rbeg].ct = time;
//Update l and h
l = h+0.001;
h = h + time_exec;
//Append again
for(i=0;i<n;i++)
{
if(q[i].at>=l && q[i].at<=h)
{
r[rend] = q[i];
rend++;
}
}
if(r[rbeg].rt == 0)
{
count++;
awt += r[rbeg].wt;
r[rbeg].tat = r[rbeg].ct - r[rbeg].at;
att += r[rbeg].tat;
rbeg++;
continue;
}
else
{
r[rend] = r[rbeg];
rend++;
rbeg++;
}
}
cout<<time;
cout<<"\n\n AWT : "<<(awt)/n<<" ATT : "<<(att)/n<<endl;
}
void irr()
{
//Local variables
int i,j,flag;
struct process q[100],r[100];
int rbeg = 0;
int rend = 0;
float l,h;
l=h=0;
int count = 0;
int time = 0;
int tq = 25;
int time_exec ;
float awt = 0,att = 0;
//Additional IRR local variables
struct process temp;//Used for swapping
float median;//Used for storing median value
int mid;
int oqt;
int n_temp = 0;//Keeps track of number of distinct processes
int exec_temp = 0;//Keeps track of number of processes executed
//Copying to another array
for(i=0;i<n;i++)
{
q[i] = p[i];
}
flag = 1;
while(count!=n)
{
//Appends the processes to the ready queue-->In the beginning
if(flag == 1)
{
for(i=0;i<n;i++)
{
if(q[i].at>=l && q[i].at<=h)
{
r[rend] = q[i];
rend++;
n_temp++;
}
}
if(rend!=0)
{
flag --;
}
else
{
l=h;
h++;
continue;
}
}
//Sort the processes in the ready queue based on bt
for(i=rbeg;i<rend;i++)
{
for(j=rbeg;j<rend-1;j++)
{
if(r[j].bt>r[j+1].bt)
{
temp = r[j];
r[j] = r[j+1];
r[j+1] = temp;
}
}
}
if(exec_temp == 0)
{
//Find the median
if((rend-rbeg)%2 == 0)
{
mid = (rend+rbeg)/2;
median = 0.5*(r[mid].bt+r[mid-1].bt);
}
else
{
mid = (rend+rbeg)/2;
median = r[mid].bt;
}
//Find the optimal time quantum
tq = (median + r[rend -1].bt)/2;
}
//Execute the process present at rbeg
cout<<"<"<<time<<">"<<"---"<<r[rbeg].pno<<"---";
exec_temp = (exec_temp+1)%(n_temp);
//cout<<"Executing : "<<r[rbeg].pno<<" From : "<<time<<" to :";
//Calculating parameters
time_exec = (r[rbeg].rt>tq)?tq:r[rbeg].rt;
r[rbeg].rt -=time_exec;
r[rbeg].wt += time - r[rbeg].ct;
time +=time_exec;
r[rbeg].ct = time;
//Update l and h
l = h+0.001;
h = h + time_exec;
//Append again
for(i=0;i<n;i++)
{
if(q[i].at>=l && q[i].at<=h)
{
r[rend] = q[i];
rend++;
}
}
if(r[rbeg].rt == 0)
{
count++;
awt += r[rbeg].wt;
r[rbeg].tat = r[rbeg].ct - r[rbeg].at;
att += r[rbeg].tat;
rbeg++;
continue;
}
else
{
r[rend] = r[rbeg];
rend++;
rbeg++;
}
}
cout<<"<"<<time<<">";
cout<<"\n\n AWT : "<<(awt)/n<<" ATT : "<<(att)/n<<endl;
}
int main()
{
//Accept input
input();
//Classical rr
cout<<"\n\n\n\n Classical RR \n\n\n";
classical_rr();
//Irr
cout<<"\n\n\n\n IRR \n\n\n";
irr();
return 0;
}