-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1c_RoundRobin.c
95 lines (82 loc) · 2.91 KB
/
1c_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
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
#include <stdio.h>
int i, n, quantum; /* give a time quantum */
void read(int b[]) {
for (i = 0; i < n; ++i) {
printf("Enter the burst time of process %d: ", i);
scanf("%d", &b[i]);
}
}
void findWaitingtime(int b[], int wt[]) {
int b_rem[20];
for (i = 0; i < n; ++i) {
b_rem[i] = b[i]; /* Create a copy of the burst time array */
}
int time = 0; /* initialize time as 0 */
while (1)
{
/* Traverse */
int flag = 0;
for (i = 0; i < n; ++i) {
if (b_rem[i] > 0) { /* continue only if burst time is greater than 0 */
flag = 1; /* there is a pending process */
if (b_rem[i] > quantum) {
time += quantum; /* shows how much time a process has been processed */
b_rem[i] -= quantum; /* Decrease the burst_time of current process by quantum */
}
else { /* If burst time is smaller than or equal to quantum. Last cycle for this process */
time += b_rem[i]; /* shows how much time a process has been processed */
wt[i] = time - b[i]; /* Waiting time is current time minus time used by this process */
b_rem[i] = 0; /* fully executed, so remaining burst time=0 */
}
}
}
if (flag == 0) { /* there is no pending process */
break;
}
}
}
void findTurnAroundtime(int tat[], int b[], int wt[]) {
for (i = 0; i < n; ++i) {
tat[i] = b[i] + wt[i];
}
}
void display(int b[], int wt[], int tat[]) {
int wtSum = 0, tatSum = 0;
printf("Process\tBurstTime WaitingTime TurnAroundTime\n");
for (i = 0; i < n; ++i) {
wtSum += wt[i];
tatSum += tat[i];
printf("%d\t%d\t\t%d\t%d\n", i, b[i], wt[i], tat[i]);
}
printf("average waiting time: %f", (float)wtSum / n);
printf("\naverage turnaround time: %f", (float)tatSum / n);
printf("\n");
}
void calcTime(int b[], int wt[], int tat[]) {
findWaitingtime(b, wt);
findTurnAroundtime(tat, b, wt);
display(b, wt, tat);
}
void main()
{
int b[20], tat[20], wt[20];
printf("Number of Processes:");
scanf("%d", &n);
read(b);
printf("Enter time quantum:");
scanf("%d", &quantum);
calcTime(b, wt, tat);
}
/* OUTPUT
Number of Processes:3
Enter the burst time of process 0: 54
Enter the burst time of process 1: 22
Enter the burst time of process 2: 6
Enter time quantum:5
Process BurstTime WaitingTime TurnAroundTime
0 54 28 82
1 22 31 53
2 6 20 26
average waiting time: 26.333334
average turnaround time: 53.666668
*/