C Program for Round Robin scheduling

Round Robin is a CPU scheduling algorithm designed for time-sharing systems. It operates like FCFS scheduling but with a crucial difference − each process gets a fixed time slice called a time quantum. When a process's time quantum expires, it is preempted and moved to the back of the ready queue, allowing other processes to execute.

What is Round Robin Scheduling?

Round Robin treats the ready queue as a circular queue where each process gets a small unit of time (10-100 milliseconds typically). The main advantages include fair CPU allocation and good response time for interactive systems. The primary disadvantage is the overhead of frequent context switching.

Key Metrics

  • Completion Time: Time when a process finishes execution
  • Turnaround Time: Completion time - Arrival time
  • Waiting Time: Turnaround time - Burst time

Syntax

void roundRobinScheduling(int processes[], int n, int burst_time[], int quantum);

Example

Consider 3 processes with time quantum = 4 milliseconds −

Process Burst Time
P1 24
P2 3
P3 3
Round Robin Gantt Chart (Quantum = 4) P1 0-4 P2 4-7 P3 7-10 P1 10-14 P1 14-18 P1 18-22 P1 22-24

Implementation

#include <stdio.h>
#include <stdbool.h>

// Function to calculate turnaround time
void turnaroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    for (int i = 0; i < n; i++)
        tat[i] = bt[i] + wt[i];
}

// Function to calculate waiting time for all processes
void waitingTime(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
    
    while (1) {
        bool done = true;
        
        for (int i = 0; i < n; i++) {
            if (rem_bt[i] > 0) {
                done = false;
                
                if (rem_bt[i] > quantum) {
                    t += quantum;
                    rem_bt[i] -= quantum;
                } else {
                    t += rem_bt[i];
                    wt[i] = t - bt[i];
                    rem_bt[i] = 0;
                }
            }
        }
        
        if (done == true)
            break;
    }
}

// Function to calculate average waiting and turnaround times
void findAvgTime(int processes[], int n, int bt[], int quantum) {
    int wt[n], tat[n], total_wt = 0, total_tat = 0;
    
    waitingTime(processes, n, bt, wt, quantum);
    turnaroundTime(processes, n, bt, wt, tat);
    
    printf("Process\tBurst Time\tWaiting Time\tTurnaround Time<br>");
    
    for (int i = 0; i < n; i++) {
        total_wt += wt[i];
        total_tat += tat[i];
        printf("P%d\t\t%d\t\t%d\t\t%d<br>", processes[i], bt[i], wt[i], tat[i]);
    }
    
    printf("\nAverage Waiting Time: %.2f", (float)total_wt / (float)n);
    printf("\nAverage Turnaround Time: %.2f<br>", (float)total_tat / (float)n);
}

int main() {
    int processes[] = {1, 2, 3};
    int n = sizeof(processes) / sizeof(processes[0]);
    int burst_time[] = {8, 6, 12};
    int quantum = 2;
    
    printf("Round Robin Scheduling (Quantum = %d)<br><br>", quantum);
    findAvgTime(processes, n, burst_time, quantum);
    
    return 0;
}
Round Robin Scheduling (Quantum = 2)

Process	Burst Time	Waiting Time	Turnaround Time
P1		8		17		25
P2		6		4		10
P3		12		6		18

Average Waiting Time: 9.00
Average Turnaround Time: 17.67

How It Works

  1. Each process gets exactly quantum time units
  2. If a process needs more time, it's preempted and queued again
  3. If a process finishes early, the next process starts immediately
  4. The algorithm continues until all processes complete

Conclusion

Round Robin scheduling ensures fairness by giving each process equal time slices. It provides good response times for interactive systems but may have higher turnaround times compared to other algorithms due to frequent context switches.

Updated on: 2026-03-15T12:30:21+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements