Write a program using C to implement Round Robin scheduling algorithm.
Below is the C program that implements the Round Robin scheduling algorithm. This scheduling algorithm allocates a fixed time unit (quantum) to each process in a cyclic order.
#include <stdio.h>
struct Process {
int id; // Process ID
int burst; // Burst time
int remaining; // Remaining time
int waiting; // Waiting time
int turnaround; // Turnaround time
};
void calculateTimes(struct Process processes[], int n, int quantum) {
int time = 0;
int complete = 0;
while (complete < n) {
for (int i = 0; i < n; i++) {
if (processes[i].remaining > 0) {
if (processes[i].remaining > quantum) {
time += quantum;
processes[i].remaining -= quantum;
} else {
time += processes[i].remaining;
processes[i].waiting = time - processes[i].burst;
processes[i].turnaround = time;
processes[i].remaining = 0;
complete++;
}
}
}
}
}
void printProcessDetails(struct Process processes[], int n) {
printf("\nProcess ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].burst,
processes[i].waiting, processes[i].turnaround);
}
}
int main() {
int n, quantum;
// Read the number of processes and time quantum
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter time quantum: ");
scanf("%d", &quantum);
struct Process processes[n];
// Read process burst times
for (int i = 0; i < n; i++) {
processes[i].id = i + 1; // Process ID
printf("Enter burst time for Process %d: ", i + 1);
scanf("%d", &processes[i].burst);
processes[i].remaining = processes[i].burst; // Initialize remaining time
}
// Calculate waiting and turnaround times
calculateTimes(processes, n, quantum);
// Print the process details
printProcessDetails(processes, n);
return 0;
}