-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundrobin.c
66 lines (56 loc) · 1.93 KB
/
roundrobin.c
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
#include <stdio.h>
int main() {
int i, sum = 0, gt[20], qt, n, bt[20], tt[20], wt[20], bt_cp[20], p[20], temp, count = 0, l = 0, k = 1, time[20] = {0};
float wt_avg = 0, tt_avg = 0;
printf("\nEnter the number of Processes(Max 20): ");
scanf("%d", &n);
printf("\nEnter the Burst Time of Each Process:\n");
for (int j = 0; j < n; j++) {
p[j] = j + 1;
printf("P%d : ", j + 1);
scanf("%d", &bt[j]);
bt_cp[j] = bt[j];
}
printf("\nEnter the Time Slice: ");
scanf("%d", &qt);
while (count != n) {
for (i = 0, count = 0; i < n; i++) {
if (bt_cp[i] == 0) {
count++;
continue;
}
if (bt_cp[i] > qt) {
bt_cp[i] -= qt;
temp = qt;
} else if (bt_cp[i] <= qt && bt_cp[i] > 0) {
temp = bt_cp[i];
bt_cp[i] = 0;
}
sum += temp;
tt[i] = sum;
gt[l++] = p[i];
time[k] = time[k - 1] + temp;
k++;
}
}
for (i = 0; i < n; i++) {
wt[i] = tt[i] - bt[i];
wt_avg += wt[i];
tt_avg += tt[i];
}
wt_avg /= n;
tt_avg /= n;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++)
printf("P%d\t\t%d\t\t%d\t\t%d\n", p[i], bt[i], wt[i], tt[i]);
printf("\nAverage Waiting Time: %.2f ms", wt_avg);
printf("\nAverage Turnaround Time: %.2f ms\n", tt_avg);
printf("\n\n\t\t\t\tGantt Chart\n\n");
printf("------------------------------------------------------------------------\n");
for (i = 0; i < l; i++)
printf("|\tP%d\t|", gt[i]);
printf("\n------------------------------------------------------------------------\n");
for (i = 0; i < k; i++)
printf("%d\t \t", time[i]);
printf("\n------------------------------------------------------------------------\n");
}