-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.cpp
More file actions
456 lines (380 loc) · 11.4 KB
/
scheduler.cpp
File metadata and controls
456 lines (380 loc) · 11.4 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// CPU Scheduler //
// Kunal Gupta 735/IT/15
// round robin is fixed :)
#include<iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<queue>
using namespace std;
struct Process // structure related to the process
{
int pid;
int pri = -1;
int AT,BT,CT,TAT,WT;
};
int ret_min(int ar[],int n) // to return the minimum of the values
{
int mi = 10000000;
int ind = 0;
for(int i=0;i<n;i++)
{
if(ar[i]<mi && ar[i]>0)
{
mi = ar[i];
ind = i;
}
}
return ind;
}
int ret_max(int ar[],int n) // to return the maximum of the values
{
int ma = -1;
int ind = 0;
for(int i=0;i<n;i++)
{
if(ar[i]>ma)
{
ma = ar[i];
ind = i;
}
}
return ind;
}
void Display(struct Process *p,int n,int indexes[],char str[]) // to display the stats and show the gantt chart
{
float avg_TAT = 0,avg_WT = 0;
cout<<"\n-------------------------"<<str<<" Scheduler------------------------------------------";
cout<<"\nPID\tPri\tAT\tBT\tCT\tTAT\tWT";
for (int i=0;i<n;i++)
{
cout<<"\n"<<p[indexes[i]].pid+1<<"\t"<<p[indexes[i]].pri<<"\t"<<p[indexes[i]].AT<<"\t"<<p[indexes[i]].BT<<"\t"<<p[indexes[i]].CT<<"\t"<<p[indexes[i]].TAT<<"\t"<<p[indexes[i]].WT;
avg_TAT+=float(p[indexes[i]].TAT);
avg_WT+=float(p[indexes[i]].WT);
}
avg_TAT/=n;
avg_WT/=n;
cout<<"\n----------------------------------------------------------------------------------";
cout<<"\nAverage TAT:"<<avg_TAT<<"\nAverage WT:"<<avg_WT;
cout<<"\n--------------------------GANTT CHART------------------------------";
cout<<"\n 0 ";
for(int i=0;i<n;i++)
{
cout<<"-- P"<<p[indexes[i]].pid+1<<" --"<<p[indexes[i]].CT;
}
}
void get_index(struct Process *p , int indexes[],int n) // this function creates an array called indexes whose elements
{ // are the process id according to sorted arrival times
int ar_AT[n];
for(int i=0;i<n;i++)
{
ar_AT[i] = p[i].AT;
}
sort(ar_AT,ar_AT+(sizeof(ar_AT)/sizeof(int)));
int f[n] = {0};
int z=0,j;
for(int i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[j].AT == ar_AT[i] && f[j]==0){ f[j] = 1;break;}
}
indexes[z++] = p[j].pid;
}
}
void fcfs(struct Process *p,int n) /// FCFS Scheduler ///
{
int indexes[n];
get_index(p,indexes,n);
p[indexes[0]].CT = p[indexes[0]].BT + p[indexes[0]].AT;
p[indexes[0]].TAT = p[indexes[0]].CT - p[indexes[0]].AT;
p[indexes[0]].WT = p[indexes[0]].TAT - p[indexes[0]].BT;
for(int i=1;i<n;i++)
{
if(p[indexes[i]].AT <= p[indexes[i-1]].CT)
{
p[indexes[i]].CT = p[indexes[i]].BT + p[indexes[i-1]].CT;
}
else
{
p[indexes[i]].CT = p[indexes[i]].BT + p[indexes[i]].AT;
}
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"FCFS");
}
int allcheck(int ar[],int n) /// to check whether all elements of the array are zero or not ( for BT)
{
for(int i=0;i<n;i++)
{
if(ar[i]!=0)
{
return 0;
}
}
return 1;
}
void sjf(struct Process*p, int n, int tq) /// shortest job first scheduler
{
int indexes[n];
get_index(p,indexes,n);
int left_bt[n]; /// this array has the remaining BT of the processes
for(int i =0;i<n;i++)
{
left_bt[i] = p[indexes[i]].BT;
}
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int i = 0;
for(i=0;i<n;i++)
{
if(p[indexes[i]].AT > init) { break;}
}
int ind = ret_min(left_bt,i);
if(left_bt[ind]<=tq){init+=left_bt[ind]; left_bt[ind] = 0;}
else
{
left_bt[ind]-=tq;
init+=tq;
}
if(left_bt[ind]==0) { p[indexes[ind]].CT = init;} /// this had to be corrected! :)
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"SJF");
}
void ljf(struct Process *p, int n)
{
int indexes[n];
get_index(p,indexes,n);
int left_bt[n]; /// this array has the remaining BT of the processes
for(int i =0;i<n;i++)
{
left_bt[i] = p[indexes[i]].BT;
}
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int i = 0;
for(i=0;i<n;i++)
{
if(p[indexes[i]].AT > init) { break;}
}
int ind = ret_max(left_bt,i);
init+=left_bt[ind];
p[indexes[ind]].CT = init;
left_bt[ind]= 0;
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"LJF");
}
void longest_rem_time_first(struct Process *p, int n, int tq)
{
int indexes[n];
get_index(p,indexes,n);
int left_bt[n]; /// this array has the remaining BT of the processes
for(int i =0;i<n;i++)
{
left_bt[i] = p[indexes[i]].BT;
}
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int i = 0;
for(i=0;i<n;i++)
{
if(p[indexes[i]].AT > init) { break;}
}
int ind = ret_max(left_bt,i);
if(left_bt[ind]<=tq){init+=left_bt[ind]; left_bt[ind] = 0;}
else
{
left_bt[ind]-=tq;
init+=tq;
}
if(left_bt[ind]==0) { p[indexes[ind]].CT = init;} /// this had to be corrected! :)
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"Longest remaining time first");
}
void round_robin(struct Process *p, int n, int tq) /// working :)
{
int indexes[n];
get_index(p,indexes,n);
queue<int> Q;
int left_bt[n]; /// this array has the remaining BT of the processes
for(int i =0;i<n;i++)
{
left_bt[i] = p[indexes[i]].BT;
}
Q.push(0);
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int k = Q.front();
Q.pop();
cout<<"\nk = "<<k;
if(left_bt[k]>tq)
{
init+=tq;
left_bt[k]-=tq;
}
else
{
init+=left_bt[k];
left_bt[k] = 0;
p[indexes[k]].CT = init;
}
cout<<"\nTime:"<<init;
if(init <= p[indexes[n-1]].AT)
{
for(int i=k+1;i<n;i++)
{
if(p[indexes[i]].AT<=init)
{
Q.push(i);
}
}
}
if(left_bt[k]>0)
{
Q.push(k);
}
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"Round Robin");
}
void priority_scheduler(struct Process *p, int n,int mode,int tq)
{
int indexes[n];
get_index(p,indexes,n);
int left_bt[n]; /// this array has the remaining BT of the processes
int priori[n];
for(int i =0;i<n;i++)
{
left_bt[i] = p[indexes[i]].BT;
priori[i] = p[indexes[i]].pri;
}
if(mode ==1) /// means non preemptive
{
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int i = 0;
for(i=0;i<n;i++)
{
if(p[indexes[i]].AT > init) { break;}
}
int ind = ret_max(priori,i); // high priority means process will be executed first
init+=left_bt[ind];
left_bt[ind] = 0;
priori[ind] = -1;
p[indexes[ind]].CT = init;
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"Priority Based ");
}
else
{
/// mode is pre emptive
int init = p[indexes[0]].AT; /// initial time is the arrival time of first process
while(!allcheck(left_bt,n))
{
int i = 0;
for(i=0;i<n;i++)
{
if(p[indexes[i]].AT > init) { break;}
}
int ind = ret_max(priori,i); // high priority means process will be executed first
if(left_bt[ind]<=tq){init+=left_bt[ind]; left_bt[ind] = 0; priori[ind] = -1;}
else
{
left_bt[ind]-=tq;
init+=tq;
}
if(left_bt[ind]==0) { p[indexes[ind]].CT = init;} /// this had to be corrected! :)
}
for(int i=0;i<n;i++)
{
p[indexes[i]].TAT = p[indexes[i]].CT - p[indexes[i]].AT;
p[indexes[i]].WT = p[indexes[i]].TAT - p[indexes[i]].BT;
}
Display(p,n,indexes,"Priority Based (Pre emptive) ");
}
}
int main()
{
int n; // no of processes
cout<<"\n------------------ CPU Process Scheduler -----------------------";
cout<<"\nSelect Your Preferred Scheduling Algorithm:";
cout<<"\n1.) First Come First Served (FCFS) \n2.) Shortest Job First(Pre Emptive) \n3.) Longest Job First";
cout<<"\n4.) Longest Remaining Time First (pre emptive) \n5.) Round Robin (pre emptive) \n6.) Priority Based ";
int choice;
cout<<"\nEnter Your Choice:";
cin>>choice;
cout<<"\nEnter the number of processes:";
cin>>n;
int tq = 0,opt;
if( choice == 2 || choice == 4 || choice == 5 || choice == 6)
{
if(choice == 6)
{
cout<<"\nSelect Priority Based Mode: 1.) Pre Emptive 2.) Non Pre Emptive";
cin>>opt;
if(opt == 1)
{
cout<<"\nEnter Time Quantum for Process execution:";
cin>>tq;
}
}
else
{
cout<<"\nEnter Time Quantum for Process execution:";
cin>>tq;
}
}
cout<<"\nEnter Process Metadata ( put priority as -1 for non priority based schedulers )";
struct Process p[n];
for(int i=0;i<n;i++)
{
cout<<"\nEnter AT and BT and Priority for Process ID#"<<i+1<<":";
cin>>p[i].AT>>p[i].BT>>p[i].pri;
p[i].pid = i;
}
switch(choice)
{
case 1:fcfs(p,n);
break;
case 2:sjf(p,n,tq);
break;
case 3:ljf(p,n);
break;
case 4:longest_rem_time_first(p,n,tq);
break;
case 5:round_robin(p,n,tq);
break;
case 6:priority_scheduler(p,n,opt-1,tq);
break;
}
return 0;
}