Round Robin
#include <stdio.h>
struct Process {
int id, a_time, w_time, c_time, b_time, t_time, r_time;
};
void display(struct Process proc[], int n, float total_w_time, float total_t_time) {
printf("\n\nProcess id\tArrival time\tBurst time\tWaiting time\tCompletion time\tTurn Around
time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].id, proc[i].a_time, proc[i].b_time,
proc[i].w_time, proc[i].c_time, proc[i].t_time);
}
printf("Average Waiting Time: %f\nAverage Turn Around Time: %f\n", (total_w_time / n),
(total_t_time / n));
}
void round_robin(struct Process proc[], int n, int tq) {
int time = 0, total_w_time = 0, total_t_time = 0, i;
printf("Gant Chart\n|");
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (proc[i].r_time > 0) {
done = 0;
if (proc[i].r_time > tq) {
time += tq;
proc[i].r_time -= tq;
} else {
time += proc[i].r_time;
proc[i].c_time = time;
proc[i].w_time = proc[i].c_time - proc[i].b_time; // Waiting time calculation
proc[i].t_time = proc[i].c_time; // Turnaround time calculation
total_t_time += proc[i].t_time;
total_w_time += proc[i].w_time;
proc[i].r_time = 0;
}
printf("P%d(%d)|", proc[i].id, time);
}
}
if (done) break;
}
display(proc, n, total_w_time, total_t_time);
}
int main() {
int n, tq;
printf("Enter the time quantum: ");
scanf("%d", &tq);
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process proc[n];
printf("Enter the process Burst time:-\n");
for (int i = 0; i < n; i++) {
proc[i].id = i + 1;
printf("Process %d: ", i + 1);
scanf("%d", &proc[i].b_time);
proc[i].a_time = 0; // Set arrival time to zero for all processes
proc[i].r_time = proc[i].b_time;
}
round_robin(proc, n, tq);
}
Output
Enter the time quantum: 4
Enter the number of processes: 3
Enter the process Burst time:-
Process 1: 20
Process 2: 4
Process 3: 6
Gant Chart
|P1(4)|P2(8)|P3(12)|P1(16)|P3(18)|P1(22)|P1(26)|P1(30)|
Process id Arrival time Burst time Waiting time Completion time Turn Around time
1 0 20 10 30 30
2 0 4 4 8 8
3 0 6 12 18 18
Average Waiting Time: 8.666667
Average Turn Around Time: 18.666666