-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process-Scheduling.cpp
526 lines (335 loc) Β· 21.3 KB
/
Process-Scheduling.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <bits/stdc++.h>
using namespace std;
// FCFS Scheduling Algorithm
class FCFS
{
public:
// Function to find the waiting Time of the Program
void findWaitingTime(vector<int> processes, int n, vector<int> bt, int wt[])
{
wt[0] = 0;
for (int i = 1; i < n; i++)
{
wt[i] = bt[i - 1] + wt[i - 1];
}
}
// Function to find TAT
void findTurnAroundTime(vector<int> processes, int n, vector<int> bt, int wt[], int tat[])
{
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
}
}
// Function to find Average time of FCFS
void findAverageTime(vector<int> processes, int n, vector<int> bt)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "\nProcesses "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";
for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "Average waiting time = " << (float)total_wt / (float)n;
cout << "\nAverage turn around time = " << (float)total_tat / (float)n;
}
};
// Priority Scheduling Algorithm
class Priority
{
public:
// Function to find the waiting Time of the Program
void findWaitingTime(vector<int> processes, int n, int wt[])
{
wt[0] = 0;
for (int i = 1; i < n; i++)
{
wt[i] = processes[i - 1] + wt[i - 1];
}
}
// Function to find TAT
void findTurnAroundTime(vector<int> processes, int n, int wt[], int tat[])
{
for (int i = 0; i < n; i++)
{
tat[i] = processes[i] + wt[i];
}
}
// Function to find Average time of Priority
void findavgTime(vector<int> processes, int n)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, wt);
findTurnAroundTime(processes, n, wt, tat);
cout << "\nProcesses "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";
for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << processes[i] << "\t\t"
<< processes[i] << "\t " << wt[i]
<< "\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
// Function to sort the processes according to priority
void priorityScheduling(vector<int> processes, int n)
{
sort(processes.begin(), processes.end(), greater<>());
cout << "Order in which processes gets executed \n";
for (int i = 0; i < n; i++)
cout << processes[i] << " ";
findavgTime(processes, n);
}
};
// Shortest Job First Scheduling Algorithm
class SJF
{
public:
// Function to find the turn around time of the Program
void findTurnAroundTimeSJF(vector<int> processes, int n, vector<int> bt, int wt[], int tat[])
{
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
}
}
// Function to find the waiting Time of the Program
void findWaitingTimeSJF(vector<int> processes, int n, vector<int> bt, int wt[])
{
for (int i = 1; i < n; i++)
{
wt[i] = wt[i - 1] + bt[i];
}
}
// Function to find Average time of SJF
void SJFFunc(vector<int> processes, int n, vector<int> bt)
{
int wt[n] = {0}; int tat[n] = {0};
findWaitingTimeSJF(processes, n, bt, wt);
findTurnAroundTimeSJF(processes, n, bt, wt, tat);
cout << "\nProcesses "
<< "BurstTime "
<< " WaitingTime "
<< "TurnAroundTime ";
cout << "\n";
for (int i = 0; i < n; i++)
{
cout << i + 1 << "\t\t" << bt[i] << "\t\t" << wt[i] << "\t" << tat[i] << "\n";
}
cout << "Average waiting time is = " << float(accumulate(wt, wt + n, 0)) / float(n) << "\n";
cout << "Average Turn Around Time is =" << float(accumulate(tat, tat + n, 0)) / float(n) << "\n";
}
};
// Round Robin Scheduling
class RR
{
public:
// Function to find the waiting Time of the Program
void findWaitingTime(vector<int> processes, int n, vector<int> bt, int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1)
{
bool done = true;
for (int i = 0; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
// Function to find the turn around Time of the Program
void findTurnAroundTime(vector<int> processes, int n,
vector<int> bt, int wt[], int tat[])
{
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
}
}
// Function to find Average time of RR
void findavgTime(vector<int> processes, int n, vector<int> bt,
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "\nProcesses "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";
for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t "
<< wt[i] << "\t\t " << tat[i] << endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
};
int main()
{
cout << R"(
ββββββ βββββββ ββββββ ββββββ ββββββ ββ βββββββ ββββββ ββββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββ βββββββ ββββββ ββββββ ββ ββ ββ βββββ ββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββββββ βββββββ ββ ββ ββ ββββββ βββββ βββββββ ββββββ ββ
)";
vector<int> processes;
int n;
cout << "Enter the number of processes: ";
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cout << "Enter The Process: ";
cin >> x;
processes.push_back(x);
}
vector<int> burst_time;
for (int i = 0; i < n; i++)
{
int y;
cout << "Enter the burst time of the processes: ";
cin >> y;
burst_time.push_back(y);
}
char ch = 'y';
do
{
cout << "Enter your choice\n";
cout << "Enter 1 for FCFS Scheduling\n";
cout << "Enter 2 for Priority Scheduling\n";
cout << "Enter 3 for RR Scheduling\n";
cout << "Enter 4 for Shortest Job First Algorihm\n";
cout << "Any other key to exit\n";
int choice;
cin >> choice;
// FCFS Scheduling Alogorithm Block
if (choice == 1)
{
FCFS f;
cout << R"(
βββββββ ββββββ βββββββ βββββββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββ ββ ββ ββ βββ ββ ββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ
βββββ ββ βββββ βββββββ βββββββ ββ βββββββ βββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββββββ ββ βββββββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββββββ βββββββ ββ ββ ββββ ββββββ
)";
f.findAverageTime(processes, n, burst_time);
}
// Priority Scheduling Block
else if (choice == 2)
{
Priority P;
cout << R"(
ββββββ ββββββ ββ ββββββ ββββββ ββ ββββββββ ββ ββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββ ββ ββ ββ βββ ββ ββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ
ββββββ ββββββ ββ ββ ββ ββββββ ββ ββ ββββ βββββββ ββ βββββββ βββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββ ββ ββ ββββββ ββ ββ ββ ββ ββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββββββ βββββββ ββ ββ ββββ ββββββ
)";
P.findavgTime(processes, n);
}
// Round Robin Block
else if (choice == 3)
{
RR r;
cout << R"(
ββββββ ββββββ ββ ββ βββ ββ ββββββ ββββββ ββββββ ββββββ ββ βββ ββ
ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ
ββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββββ ββ ββ ββββββ ββ ββ ββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββ ββββββ ββββββ ββ ββββ ββββββ ββ ββ ββββββ ββββββ ββ ββ ββββ
)";
r.findavgTime(processes, n, burst_time, 3);
}
// SJF Block
else if (choice == 4)
{
SJF s;
cout << R"(
βββββββ ββ ββ ββββββ ββββββ ββββββββ βββββββ βββββββ ββββββββ ββ ββββββ ββββββ βββββββ ββ ββββββ βββββββ ββββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ βββββββ ββ ββ ββββββ ββ βββββ βββββββ ββ ββ ββ ββ ββββββ βββββ ββ ββββββ βββββββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ ββ ββ ββββββ ββ ββ ββ βββββββ βββββββ ββ βββββ ββββββ ββββββ ββ ββ ββ ββ βββββββ ββ
)";
s.SJFFunc(processes, n, burst_time);
}
// Default Case
else
{
cout << "\n";
cout << R"(
βββββββ ββ ββ ββ ββββββββ ββ βββ ββ ββββββ
ββ ββ ββ ββ ββ ββ ββββ ββ ββ
βββββ βββ ββ ββ ββ ββ ββ ββ ββ βββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ ββ ββ ββ ββ ββ ββ ββββ ββββββ ββ ββ ββ
)";
exit(0);
}
cout << "\n\nWant to Enter More?, Enter y or n\n";
cin >> ch;
} while (ch == 'y');
}
/*
ββββββ ββββββ ββββββ ββββββ βββββββ βββββββ βββββββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββ ββ ββ ββ βββ ββ ββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ
ββββββ ββββββ ββ ββ ββ βββββ βββββββ βββββββ βββββββ ββ βββββββ βββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββ ββ ββββββ ββββββ βββββββ βββββββ βββββββ βββββββ ββββββ ββ ββ βββββββ ββββββ ββββββ βββββββ ββ ββ ββββ ββββββ
ββββββ ββ ββ
ββ ββ ββ ββ ββ
ββββββ ββββ
ββ ββ ββ ββ
ββββββ ββ
βββββββ βββββ ββ ββ ββββββ βββββ ββββββ ββ ββ ββ ββ ββ ββ βββ βββ βββββ ββββββ ββ ββ ββ βββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ βββββββ ββ ββ ββββββ βββββββ ββββββ βββββββ βββββ ββ ββ ββ ββββ ββ βββββββ ββββββ ββ βββββββ βββββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββββββ ββ ββ ββ ββ ββββββ ββ ββ ββ ββ ββ ββ βββββ ββ ββ ββ ββ
βββββ ββββββ ββ ββββββββ ββ ββ βββββ ββββββ βββββ βββ ββ ββββββ βββββββ ββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ
βββββββ ββ ββ ββ ββ ββββ βββββββ ββββββ βββββββ ββ ββ ββ ββ ββ βββββ ββββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
ββ ββ ββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββββ βββββββ ββ
*/