-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscheduling.cpp
910 lines (798 loc) · 22.9 KB
/
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
#include <bits/stdc++.h>
using namespace std;
void roundRobin()
{
cout << "Enter the number of processes: ";
int n, i, j;
cin >> n;
while (n < 0)
{
cout << "Wrong input, try again\nEnter number of processes: ";
cin >> n;
}
vector<tuple<int, int, int, int, int>> times(n); // {at, bt, ct, tat, wt}
for (i = 0; i < n; i++)
{
int t;
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival Time: ";
cin >> t;
while (t < 0)
{
cout << "Wrong input, try again\nArrival time: ";
cin >> t;
}
get<0>(times[i]) = t; // Arrival time
cout << "Burst Time: ";
cin >> t;
while (t < 0)
{
cout << "Wrong input, try again\nBurst time: ";
cin >> t;
}
cout << endl;
get<1>(times[i]) = t; // Burst time
get<2>(times[i]) = 0; // Completion time
get<3>(times[i]) = 0; // Turn around time
get<4>(times[i]) = 0; // Waiting time
}
int ti = INT_MAX; // current time
int slicetime;
cout << "Enter slice time: ";
cin >> slicetime;
while (slicetime <= 0)
{
cout << "Wrong input, try again\nEnter slice time: ";
cin >> slicetime;
}
for (i = 0; i < n; i++)
{
ti = min(ti, get<0>(times[i]));
}
int visited[n] = {0}, bt[n]; // completed programs and Burst time
for (i = 0; i < n; i++)
{
bt[i] = get<1>(times[i]);
}
while (1)
{
int f = 0;
for (i = 0; i < n; i++)
{
if (visited[i])
continue;
if (get<0>(times[i]) <= ti && get<1>(times[i]) > 0)
{
f = 1;
ti += min(bt[i], slicetime);
bt[i] = max(0, bt[i] - slicetime);
if (bt[i] == 0)
{
get<2>(times[i]) = ti;
get<3>(times[i]) = get<2>(times[i]) - get<0>(times[i]);
get<4>(times[i]) = get<3>(times[i]) - get<1>(times[i]);
visited[i] = 1;
}
}
}
if (f == 0)
{
ti++;
}
f = 0;
// checking if all programs completed
for (i = 0; i < n; i++)
{
if (visited[i] == 0)
{
f = 1;
break;
}
}
if (f == 0)
break;
}
double TAT = 0, TWT = 0;
for (i = 0; i < n; i++)
{
TAT += get<3>(times[i]);
TWT += get<4>(times[i]);
}
cout << "\nProcess No.\tArrival Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\n";
for (i = 0; i < n; i++)
{
cout << i + 1 << "\t\t" << get<0>(times[i]) << "\t\t" << get<1>(times[i]) << "\t\t" << get<2>(times[i]) << "\t\t\t" << get<3>(times[i]) << "\t\t\t" << get<4>(times[i]) << "\n";
}
TAT = TAT / (1.0 * n); // Average TAT
TWT = TWT / (1.0 * n); // Average WT
cout << "\nAverage Turn around time is: " << TAT << "\n";
cout << "Average Waiting time is: " << TWT << "\n";
return;
}
void sjf()
{
int size;
cout << "Enter the number of processes: ";
cin >> size;
int at[size]; //arrival time of each process
int bt[size]; //burst time of each process
for (int i = 0; i < size; i++)
{
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival time: ";
cin >> at[i];
cout << "Burst time: ";
cin >> bt[i];
cout << endl;
}
long long wt[size], tat[size], total_WT = 0, total_TAT = 0;
//finding waiting time
long long rt[size];
for (int i = 0; i < size; i++)
rt[i] = bt[i];
long long comp = 0, t = 0, minm = INT_MAX;
long long shortest = 0, fin_time;
bool check = false;
while (comp != size)
{
for (int j = 0; j < size; j++)
{
if ((at[j] <= t) && (rt[j] < minm) && rt[j] > 0)
{
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false)
{
t++;
continue;
}
// decrementing the remaining time
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// If a process gets completly executed
if (rt[shortest] == 0)
{
comp++;
check = false;
fin_time = t + 1;
// Calculate waiting time
wt[shortest] = fin_time - bt[shortest] - at[shortest];
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
//turn around time
for (int i = 0; i < size; i++)
tat[i] = bt[i] + wt[i];
cout << "\nProcess No.\tArrival Time\tBurst Time\tTurn-around Time\tWaiting Time\n";
for (int i = 0; i < size; i++)
{
total_TAT += tat[i];
total_WT += wt[i];
cout << i + 1 << "\t\t" << at[i] << "\t\t" << bt[i] << "\t\t" << tat[i] << "\t\t\t" << wt[i] << "\n";
}
cout << "\nAverage Turn around time is: " << (double)total_TAT / size << "\n";
cout << "Average Waiting time is: " << (double)total_WT / size << "\n";
}
void fcfs()
{
cout << "\nEnter the number of processes: ";
int n, i;
cin >> n;
while (n < 0)
{
cout << "Wrong input, try again"
<< "\nEnter number of processes: ";
cin >> n;
}
vector<tuple<int, int, int, int, int>> times(n); // {at, bt, ct, tat, wt}
cout << "Process Details" << endl
<< endl;
for (i = 0; i < n; i++)
{
int t;
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival time: ";
cin >> t;
while (t < 0)
{
cout << "Wrong input, try again\nArrival Time P" << i << ": ";
cin >> t;
}
get<0>(times[i]) = t; // Arrival time
cout << "Burst time: ";
cin >> t;
while (t < 0)
{
cout << "Wrong input, try again\nBurst Time P" << i << ": ";
cin >> t;
}
cout << endl;
get<1>(times[i]) = t; // Burst time
get<2>(times[i]) = 0; // Completion time
get<3>(times[i]) = 0; // Turn around time
get<4>(times[i]) = 0; // Waiting time
}
sort(times.begin(), times.end()); // sorting according to Arrival time
int ti = 0; // current time
for (i = 0; i < n; i++)
{
if (ti < get<0>(times[i]))
ti = get<0>(times[i]);
get<2>(times[i]) = ti + get<1>(times[i]);
ti += get<1>(times[i]);
get<3>(times[i]) = get<2>(times[i]) - get<0>(times[i]);
get<4>(times[i]) = get<3>(times[i]) - get<1>(times[i]);
}
double TAT = 0, TWT = 0;
for (i = 0; i < n; i++)
{
TAT += get<3>(times[i]);
TWT += get<4>(times[i]);
}
cout << "\n"
"Process No.\tArrival Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\n";
for (i = 0; i < n; i++)
{
cout << i + 1 << "\t\t" << get<0>(times[i]) << "\t\t" << get<1>(times[i]) << "\t\t" << get<2>(times[i]) << "\t\t\t" << get<3>(times[i]) << "\t\t\t" << get<4>(times[i]) << "\n";
}
TAT = TAT / (1.0 * n); // Average TAT
TWT = TWT / (1.0 * n); // Average WT
cout << "\nAverage Turn around time is: " << TAT << "\n";
cout << "Average Waiting time is : " << TWT << "\n";
return;
}
void LJF()
{
struct processes
{
int pid;
int at;
int bt;
int ct;
int tat;
int wt;
};
int n, i, j, sumtat = 0, sumwt = 0;
cout << "Enter no of processes" << endl;
cin >> n;
struct processes arr[n];
struct processes k; // temporary structure used in swapping
for (i = 0; i < n; i++)
{
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival time: ";
cin >> arr[i].at;
cout << "Burst time: ";
cin >> arr[i].bt;
arr[i].pid = i + 1;
cout << endl;
}
//sorting the array of structures according to arrival time and if arrival time is same then sorting it according to processid
for (i = 0; i < n; i++)
{
for (j = 0; j < n - 1; j++)
{
if (arr[j].at > arr[j + 1].at)
{
k = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = k;
}
else if (arr[j].at == arr[j + 1].at)
{
if (arr[j].pid > arr[j + 1].pid)
{
k = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = k;
}
}
}
}
//finding the task which will be executed first
int maxt, l = 0;
maxt = arr[0].bt;
for (i = 1; arr[i].at == arr[0].at; i++)
{
if (arr[i].bt > maxt)
{
maxt = arr[i].bt;
l = i;
}
}
k = arr[0];
arr[0] = arr[l];
arr[l] = k;
arr[0].ct = arr[0].at + arr[0].bt;
//sorting the array of structures according to largest burst times for arrival times less than the previous completion time
for (i = 1; i < n; i++)
{
maxt = arr[i].bt;
int val = i;
for (j = i; j < n; j++)
{
if (arr[j].at <= arr[i - 1].ct && arr[j].bt > maxt)
{
maxt = arr[j].bt;
val = j;
}
}
k = arr[i];
arr[i] = arr[val];
arr[val] = k;
//takes account of the case where if all the arrival times are greater than previous completion time
if (arr[i].at > arr[i - 1].ct)
{
arr[i].ct = arr[i].at + arr[i].bt;
}
else
{
arr[i].ct = arr[i - 1].ct + arr[i].bt;
}
}
//finding the turnaround time and the waiting time
for (i = 0; i < n; i++)
{
arr[i].tat = arr[i].ct - arr[i].at;
arr[i].wt = arr[i].tat - arr[i].bt;
sumtat += arr[i].tat;
sumwt += arr[i].wt;
}
cout << "\nProcess No.\tArrival Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\n";
for (i = 0; i < n; i++)
{
cout << arr[i].pid << "\t\t" << arr[i].at << "\t\t" << arr[i].bt << "\t\t" << arr[i].ct << "\t\t\t" << arr[i].tat << "\t\t\t" << arr[i].wt << endl;
}
cout << "\nThe average turnaround time is: " << float(sumtat) / float(n) << endl;
cout << "The average waiting time is: " << float(sumwt) / float(n) << endl;
}
//
void LRTF()
{
struct processes
{
int pid;
int at;
int bt;
int ct;
int tat;
int wt;
int rembt;
};
int n, i, j, sumtat = 0, sumwt = 0, timeslice;
cout << "Enter no of processes: ";
cin >> n;
cout << "Enter the timeslice: ";
cin >> timeslice;
struct processes arr[n];
struct processes k; // temporary structure used in swapping
for (i = 0; i < n; i++)
{
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival time: ";
cin >> arr[i].at;
cout << "Burst time: ";
cin >> arr[i].bt;
arr[i].rembt = arr[i].bt;
arr[i].pid = i + 1;
cout << endl;
}
//sorting the array of structures according to arrival time and if arrival time is same then sorting it according to processid
for (i = 0; i < n; i++)
{
for (j = 0; j < n - 1; j++)
{
if (arr[j].at > arr[j + 1].at)
{
k = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = k;
}
else if (arr[j].at == arr[j + 1].at)
{
if (arr[j].pid > arr[j + 1].pid)
{
k = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = k;
}
}
}
}
//finding the task which will be executed first
int maxt, l = 0;
maxt = arr[0].bt;
for (i = 1; arr[i].at == arr[0].at; i++)
{
if (arr[i].bt > maxt)
{
maxt = arr[i].bt;
l = i;
}
}
k = arr[0];
arr[0] = arr[l];
arr[l] = k;
int comptasks = 0, currtime = 0;
l = 0;
bool chk[n] = {false};
while (comptasks != n)
{
if (arr[l].at > currtime)
{
currtime = arr[l].at;
}
if (arr[l].rembt <= timeslice)
{
currtime += arr[l].rembt;
arr[l].rembt = 0;
arr[l].ct = currtime;
comptasks++;
}
else
{
currtime += timeslice;
arr[l].rembt -= timeslice;
}
maxt = arr[l].rembt;
for (i = 0; i < n; i++)
{
if (arr[i].at <= currtime && arr[i].rembt > maxt)
{
maxt = arr[i].rembt;
l = i;
}
else if (arr[i].at <= currtime && arr[i].rembt == maxt && arr[i].rembt != 0)
{
if (i < l)
{
l = i;
}
}
}
if (maxt == 0)
{
for (i = 0; i < n; i++)
{
if (arr[i].rembt > 0)
{
l = i;
break;
}
}
}
}
//finding the turnaround time and the waiting time
for (i = 0; i < n; i++)
{
arr[i].tat = arr[i].ct - arr[i].at;
arr[i].wt = arr[i].tat - arr[i].bt;
sumtat += arr[i].tat;
sumwt += arr[i].wt;
}
cout << "The table is as follows: (its shown according to the process that happens first) " << endl;
cout << "\nProcess No.\tArrival Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\n";
for (i = 0; i < n; i++)
{
cout << "\t" << arr[i].pid << "\t\t" << arr[i].at << "\t\t" << arr[i].bt << "\t\t" << arr[i].ct << "\t\t\t\t" << arr[i].tat << "\t\t" << arr[i].wt << endl;
}
cout << "The average turnaround time is: " << float(sumtat) / float(n) << endl;
cout << "The average waiting time is: " << float(sumwt) / float(n) << endl;
}
struct priority_process
{
int bt, at, ct, wt, st, pno, tt, cbt, pr;
};
int get(struct priority_process arr[], int t, int n)
{
int imin, min = 9999, i;
for (i = 0; i < n; i++)
{
if (arr[i].at <= t && arr[i].st == 0)
if (min > arr[i].pr)
{
min = arr[i].pr;
imin = i;
}
}
return imin;
}
void final_output(struct priority_process arr[], int p[], int n, int nop)
{
int i, a[100], s = 0;
float avgtat = 0, avgwt = 0;
for (i = 0; i < n; i++)
{
while (i < n - 1 && p[i] == p[i + 1])
{
s++;
i++;
}
s++;
arr[p[i]].wt = s - arr[p[i]].at - arr[p[i]].tt;
}
for (i = 0; i < nop; i++)
{
arr[i].tt += arr[i].wt;
avgwt += arr[i].wt;
avgtat += arr[i].tt;
}
printf("\n\n");
printf("Process\t|Priority\t|Arrival Time\t|Burst Time\t|Completion Time|Turnaround Time|Waiting Time\n");
for (i = 0; i < nop; i++)
{
printf("[P%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n", arr[i].pno, arr[i].pr, arr[i].at,
arr[i].cbt, arr[i].ct, arr[i].tt, arr[i].wt);
}
printf("\n");
printf("Total wait time: %.2f\n", avgwt);
printf("Total turnaround time: %.2f\n", avgtat);
avgwt = avgwt / nop;
avgtat = avgtat / nop;
printf("Average Waiting Time : %.2f\n", avgwt);
printf("Average Turnaround Time : %.2f\n", avgtat);
return;
}
int iscomplete(struct priority_process arr[], int n)
{
int i;
for (i = 0; i < n; i++)
if (arr[i].st == 0)
return 0;
return 1;
}
void priority_premptive()
{
int n, i, a, t = 0, j;
int p[100];
float avgwt = 0, avgtat = 0;
struct priority_process arr[100];
printf("Enter Number of Processes\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter Arrival Time, priority & Burst Time for Process [P%d]\n", i);
scanf("%d%d%d", &arr[i].at, &arr[i].pr, &arr[i].bt);
arr[i].pno = i;
arr[i].cbt = arr[i].bt;
arr[i].st = 0;
arr[i].tt = arr[i].bt;
arr[i].wt = 0;
}
i = 0;
while (1)
{
if (iscomplete(arr, n))
break;
a = get(arr, t, n);
p[i] = a;
arr[a].bt -= 1;
if (arr[a].bt == 0)
{
arr[a].st = 1;
arr[a].ct = t + 1;
}
t = t + 1;
i++;
}
final_output(arr, p, i, n);
return;
}
//hrrn
struct node
{
int pname;
int btime;
int atime;
int wtime;
float rr = 0;
} a[50];
void insert(int n)
{
int i;
for (int i = 0; i < n; i++)
{
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival time: ";
cin >> a[i].atime;
cout << "Burst time: ";
cin >> a[i].btime;
a[i].rr = 0;
a[i].pname = i + 1;
a[i].wtime = -a[i].atime;
cout << endl;
}
}
bool btimeSort(node a, node b)
{
return a.btime < b.btime;
}
bool atimeSort(node a, node b)
{
return a.atime < b.atime;
}
bool rrtimeSort(node a, node b)
{
return a.rr > b.rr;
}
void disp(int n)
{
sort(a, a + n, btimeSort);
sort(a, a + n, atimeSort);
int ttime = 0, i;
int j, tArray[n];
for (i = 0; i < n; i++)
{
j = i;
while (a[j].atime <= ttime && j != n)
{
j++;
}
for (int q = i; q < j; q++)
{
a[q].wtime = ttime - a[q].atime;
a[q].rr = (float)(a[q].wtime + a[q].btime) / (float)a[q].btime;
}
sort(a + i, a + j, rrtimeSort);
tArray[i] = ttime;
cout << endl;
ttime += a[i].btime;
}
tArray[i] = ttime;
float averageWaitingTime = 0;
float averageResponseTime = 0;
float averageTAT = 0;
cout << "\nProcess No.\tArrival Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\n";
for (i = 0; i < n; i++)
{
cout << 'P' << a[i].pname << "\t\t";
cout << a[i].atime << "\t\t";
cout << a[i].btime << "\t\t";
cout << tArray[i + 1] << "\t\t\t ";
cout << tArray[i] - a[i].atime + a[i].btime << "\t\t\t" << a[i].wtime << endl;
averageTAT += tArray[i] - a[i].atime + a[i].btime;
averageWaitingTime += tArray[i] - a[i].atime;
averageResponseTime += tArray[i] - a[i].atime;
}
cout << "\n";
cout << "\n";
cout << "Average Response time: " << (float)averageResponseTime / (float)n << endl;
cout << "Average Waiting time: " << (float)averageWaitingTime / (float)n << endl;
cout << "Average TA time: " << (float)averageTAT / (float)n << endl;
}
void hrrn()
{
int nop, choice, i;
cout << "Enter number of processes: ";
cin >> nop;
insert(nop);
disp(nop);
return;
}
//SRTF
struct process_srtf
{
int pid;
int arrival_time;
int burst_time;
int start_time;
int completion_time;
int turnaround_time;
int waiting_time;
int response_time;
};
void srtf()
{
int n;
struct process_srtf p[100];
float avg_turnaround_time;
float avg_waiting_time;
float avg_response_time;
float cpu_utilisation;
int total_turnaround_time = 0;
int total_waiting_time = 0;
int total_response_time = 0;
int total_idle_time = 0;
float throughput;
int burst_remaining[100];
int is_completed[100];
memset(is_completed, 0, sizeof(is_completed));
cout << setprecision(2) << fixed;
cout << "Enter the number of processes: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "For process " << i + 1 << ":" << endl;
cout << "Arrival Time: ";
cin >> p[i].arrival_time;
cout << "Burst time"
<< ": ";
cin >> p[i].burst_time;
p[i].pid = i + 1;
burst_remaining[i] = p[i].burst_time;
cout << endl;
}
int current_time = 0;
int completed = 0;
int prev = 0;
while (completed != n)
{
int idx = -1;
int mn = 10000000;
for (int i = 0; i < n; i++)
{
if (p[i].arrival_time <= current_time && is_completed[i] == 0)
{
if (burst_remaining[i] < mn)
{
mn = burst_remaining[i];
idx = i;
}
if (burst_remaining[i] == mn)
{
if (p[i].arrival_time < p[idx].arrival_time)
{
mn = burst_remaining[i];
idx = i;
}
}
}
}
if (idx != -1)
{
if (burst_remaining[idx] == p[idx].burst_time)
{
p[idx].start_time = current_time;
total_idle_time += p[idx].start_time - prev;
}
burst_remaining[idx] -= 1;
current_time++;
prev = current_time;
if (burst_remaining[idx] == 0)
{
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
p[idx].response_time = p[idx].start_time - p[idx].arrival_time;
total_turnaround_time += p[idx].turnaround_time;
total_waiting_time += p[idx].waiting_time;
total_response_time += p[idx].response_time;
is_completed[idx] = 1;
completed++;
}
}
else
{
current_time++;
}
}
int min_arrival_time = 10000000;
int max_completion_time = -1;
for (int i = 0; i < n; i++)
{
min_arrival_time = min(min_arrival_time, p[i].arrival_time);
max_completion_time = max(max_completion_time, p[i].completion_time);
}
avg_turnaround_time = (float)total_turnaround_time / n;
avg_waiting_time = (float)total_waiting_time / n;
avg_response_time = (float)total_response_time / n;
// cpu_utilisation = ((max_completion_time - total_idle_time) / (float)max_completion_time) * 100;
// throughput = float(n) / (max_completion_time - min_arrival_time);
cout << endl
<< endl;
cout << "\nProcess No.\tArrival Time\tStart Time\tBurst Time\tCompletion Time\t\tTurn-around Time\tWaiting Time\tResponse Time\n";
for (int i = 0; i < n; i++)
{
cout << p[i].pid << "\t\t" << p[i].arrival_time << "\t\t" << p[i].start_time << "\t\t" << p[i].burst_time << "\t\t" << p[i].completion_time << "\t\t\t" << p[i].turnaround_time << "\t\t\t" << p[i].waiting_time << "\t\t" << p[i].response_time << "\n";
}
cout << "\nAverage Turnaround Time = " << avg_turnaround_time << endl;
cout << "Average Waiting Time = " << avg_waiting_time << endl;
cout << "Average Response Time = " << avg_response_time << endl;
}