CPU scheduling is a crucial component of modern operating systems, determining how processes share CPU time efficiently. One of the most commonly used scheduling algorithms is Round Robin (RR), which ensures fairness by allocating a fixed time quantum to each process in a cyclic order. In this blog post, we'll explore the key concepts of Round Robin scheduling and why it's an essential technique for time-sharing systems.
What is Round Robin Scheduling?
Round Robin is a preemptive CPU scheduling algorithm that assigns a fixed time slice (quantum) to processes in a queue. If a process doesn’t complete within its allotted time, it is moved to the end of the queue, and the next process gets CPU time. This cycle continues until all processes finish execution. The primary advantage of Round Robin scheduling is fairness, preventing any single process from monopolizing the CPU.
Herein, we will provide a Round Robin CPU Scheduling Program in C and a Round Robin CPU Scheduling Program in C with Gantt Chart to help visualize process execution and understand the scheduling mechanism effectively.
Round Robin CPU Scheduling Program in C
#include stdio.h "here include the <> in stdio.h"
// Function to find the waiting time for all processes
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner until all of them are not done.
while (1) {
int done = 1;
// Traverse all processes one by one repeatedly
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0; // There is a pending process
if (rem_bt[i] > quantum) {
// Increase the value of t i.e. shows how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process by quantum
rem_bt[i] -= quantum;
} else {
// Increase the value of t i.e. shows how much time a process has been processed
t = t + rem_bt[i];
// Waiting time is current time minus time used by this process
wt[i] = t - bt[i];
// As the process gets fully executed make its remaining burst time 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == 1)
break;
}
}
// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
// calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time
void findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
printf("Processes\tBurst Time\tWaiting Time\tTurnaround Time\n");
// Calculate total waiting time and total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
printf("Average turnaround time = %.2f\n", (float)total_tat / (float)n);
}
// Driver code
int main() {
// Number of processes
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n];
int burst_time[n];
// Input burst times for each process
for (int i = 0; i < n; i++) {
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
processes[i] = i + 1; // Process IDs
}
// Time quantum
int quantum;
printf("Enter the time quantum: ");
scanf("%d", &quantum);
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Round Robin CPU Scheduling Program in C with Gantt Chart
#include stdio.h "do not forget to include <> in include library"
// Function to find the waiting time, turnaround time, and Gantt chart
void findWaitingTime(int processes[], int n, int bt[], int wt[], int tat[], int quantum) {
int rem_bt[n]; // Remaining burst time
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
int done;
// Array to store the Gantt chart
int gantt[100][2]; // [process, time]
int gantt_index = 0;
// Keep traversing processes in round robin manner until all are done
while (1) {
done = 1;
// Traverse all processes
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0; // There is a pending process
if (rem_bt[i] > quantum) {
// Process executes for the time quantum
t += quantum;
rem_bt[i] -= quantum;
// Store in Gantt chart
gantt[gantt_index][0] = processes[i];
gantt[gantt_index][1] = quantum;
gantt_index++;
} else {
// Process executes for the remaining time
t += rem_bt[i];
wt[i] = t - bt[i]; // Waiting time = current time - burst time
rem_bt[i] = 0;
// Store in Gantt chart
gantt[gantt_index][0] = processes[i];
gantt[gantt_index][1] = rem_bt[i] + quantum;
gantt_index++;
}
}
}
// If all processes are done
if (done == 1)
break;
}
// Calculate turnaround time
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
// Print Gantt chart
printf("\nGantt Chart:\n");
for (int i = 0; i < gantt_index; i++) {
printf("P%d ", gantt[i][0]);
}
printf("\n");
// Print time intervals
printf("Time Intervals:\n");
int current_time = 0;
for (int i = 0; i < gantt_index; i++) {
printf("%d - %d: P%d\n", current_time, current_time + gantt[i][1], gantt[i][0]);
current_time += gantt[i][1];
}
}
// Function to calculate average time
void findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n];
int total_wt = 0, total_tat = 0;
// Calculate waiting time and turnaround time
findWaitingTime(processes, n, bt, wt, tat, quantum);
// Display processes along with all details
printf("\nProcesses\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage waiting time = %.2f\n", (float)total_wt / (float)n);
printf("Average turnaround time = %.2f\n", (float)total_tat / (float)n);
}
// Driver code
int main() {
int n; // Number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n];
int burst_time[n];
// Input burst times for each process
for (int i = 0; i < n; i++) {
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
processes[i] = i + 1; // Process IDs
}
// Time quantum
int quantum;
printf("Enter the time quantum: ");
scanf("%d", &quantum);
findavgTime(processes, n, burst_time, quantum);
return 0;
}