-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_roundRobin.c
63 lines (61 loc) · 2.07 KB
/
5_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
/*
5. Write a C program to simulate the Round Robin CPU scheduling algorithms.
*/
#include <stdio.h>
int main() {
int i, limit, totalTime = 0;
int x, counter = 0, quantum;
int waitTime = 0;
int turnaroundTime = 0;
int arrivalTime[10];
int burstTime[10];
int temp[10];
float avgWaitTime;
float avgTurnaroundTime;
printf("\nEnter Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++) {
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time: \t");
scanf("%d", &arrivalTime[i]);
printf("Burst Time: \t");
scanf("%d", &burstTime[i]);
temp[i] = burstTime[i];
}
printf("\nEnter Time Quantum: \t");
scanf("%d", &quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(totalTime = 0, i = 0; x != 0; ) {
if((temp[i] <= quantum) && (temp[i] > 0)) {
totalTime = (totalTime + temp[i]);
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0) {
temp[i] = (temp[i] - quantum);
totalTime = (totalTime + quantum);
}
if((temp[i] == 0) && (counter == 1)) {
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d",
(i + 1), burstTime[i], (totalTime - arrivalTime[i]),
(totalTime - arrivalTime[i] - burstTime[i]));
waitTime = (waitTime + totalTime - arrivalTime[i] - burstTime[i]);
turnaroundTime = (turnaroundTime + totalTime - arrivalTime[i]);
counter = 0; // set the counter to 0
}
if(i == limit - 1) {
i = 0;
} else if(arrivalTime[i + 1] <= totalTime) {
i++;
} else {
i = 0;
}
}
avgWaitTime = (waitTime * 1.0 / limit);
avgTurnaroundTime = (turnaroundTime * 1.0 / limit);
printf("\n\nAverage Waiting Time:\t%f", avgWaitTime);
printf("\nAverage Turnaround Time:\t%f\n", avgTurnaroundTime);
return 0;
}