Skip to content

Commit 044891e

Browse files
Round-robin-algorithm-added
1 parent ebfa887 commit 044891e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Program to execute Round Robin algorithm to perform process scheduling.
3+
*/
4+
#include <stdio.h>
5+
6+
int main()
7+
{
8+
9+
int p_id, n = 0, j, time, remaining, flag = 0, time_quantum;
10+
/*
11+
at = Ariving Time
12+
bt = Burst Time
13+
rt = Remaining Burst Time
14+
remaining = remaining processes
15+
p_id = process id
16+
*/
17+
int wait_time = 0, turnaround_time = 0, at[10], bt[10], rt[10];
18+
printf("Enter Total number of Processes: ");
19+
scanf("%d", &n);
20+
remaining = n;
21+
for (p_id = 0; p_id < n; p_id++)
22+
{
23+
printf("Enter Arrival Time and Burst Time for Process process number %d :", p_id + 1);
24+
scanf("%d", &at[p_id]);
25+
scanf("%d", &bt[p_id]);
26+
rt[p_id] = bt[p_id];
27+
}
28+
printf("Enter Time Quantum:\t");
29+
scanf("%d", &time_quantum);
30+
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
31+
for (time = 0, p_id = 0; remaining != 0;)
32+
{
33+
if (rt[p_id] <= time_quantum && rt[p_id] > 0)
34+
{
35+
time += rt[p_id];
36+
rt[p_id] = 0;
37+
flag = 1;
38+
}
39+
else if (rt[p_id] > 0)
40+
{
41+
rt[p_id] -= time_quantum;
42+
time += time_quantum;
43+
}
44+
if (rt[p_id] == 0 && flag == 1)
45+
{
46+
remaining--;
47+
printf("P[%d]\t|\t%d\t|\t%d\n", p_id + 1, time - at[p_id], time - at[p_id] - bt[p_id]);
48+
wait_time += time - at[p_id] - bt[p_id];
49+
turnaround_time += time - at[p_id];
50+
flag = 0;
51+
}
52+
if (p_id == n - 1)
53+
p_id = 0;
54+
else if (at[p_id + 1] <= time)
55+
p_id++;
56+
else
57+
p_id = 0;
58+
}
59+
printf("\nAverage Waiting Time= %f\n", wait_time * 1.0 / n);
60+
printf("Average Turnaround Time = %f", turnaround_time * 1.0 / n);
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)