Round Robin Scheduling in C – Preemptive CPU Algorithm

Round Robin (RR) is a preemptive CPU scheduling algorithm designed for time-sharing systems. Instead of running one process to completion like FCFS, the CPU gives every process a fixed slice of time called a time quantum. If a process doesn’t finish within that slice, it’s paused and moved to the back of the queue, and the next process gets its turn. This keeps the system responsive — no single process can hog the CPU.

Round Robin is the scheduling algorithm behind most modern time-sharing operating systems. This page covers the algorithm, a complete C program, a worked trace, and its complexity.

How Round Robin Works

  1. Pick a fixed time quantum (e.g. 2 or 4 time units).
  2. Give the CPU to the next unfinished process in the queue.
  3. Let it run for up to one quantum. If it finishes before the quantum is used up, record its completion.
  4. If it still has work left, pause it and move to the next process — it rejoins the back of the rotation.
  5. Repeat until every process has completed.

Step-by-step trace

Three processes, all ready at time 0, quantum = 2:

Process   Burst
P1        10
P2        5
P3        8

Round 1: P1 runs 0-2 (8 left), P2 runs 2-4 (3 left), P3 runs 4-6 (6 left)
Round 2: P1 runs 6-8 (6 left), P2 runs 8-10 (1 left), P3 runs 10-12 (4 left)
Round 3: P1 runs 12-14 (4 left), P2 runs 14-15 and FINISHES, P3 runs 15-17 (2 left)
Round 4: P1 runs 17-19 (2 left), P3 runs 19-21 and FINISHES
Round 5: P1 runs 21-23 and FINISHES

C Program for Round Robin Scheduling

/* Round Robin CPU Scheduling in C
 * Compile: gcc -ansi -Wall -Wextra rr.c -o rr */
#include <stdio.h>

#define MAX 20

int main(void)
{
    int burst[MAX], remaining[MAX];
    int waiting[MAX], turnaround[MAX];
    int n, quantum, i, time = 0, done;
    double total_wt = 0.0, total_tat = 0.0;

    printf("Enter number of processes: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Process %d - Burst Time: ", i + 1);
        scanf("%d", &burst[i]);
        remaining[i] = burst[i];
        waiting[i] = 0;
    }

    printf("Enter time quantum: ");
    scanf("%d", &quantum);

    /* Repeatedly scan the process list, running each unfinished
       process for up to `quantum` units, until all are done. */
    do {
        done = 1;
        for (i = 0; i < n; i++) {
            if (remaining[i] > 0) {
                done = 0;
                if (remaining[i] > quantum) {
                    time += quantum;
                    remaining[i] -= quantum;
                } else {
                    time += remaining[i];
                    waiting[i] = time - burst[i];
                    remaining[i] = 0;
                }
            }
        }
    } while (!done);

    printf("\nPID\tBurst\tWaiting\tTurnaround\n");
    for (i = 0; i < n; i++) {
        turnaround[i] = waiting[i] + burst[i];
        total_wt += waiting[i];
        total_tat += turnaround[i];
        printf("%d\t%d\t%d\t%d\n", i + 1, burst[i], waiting[i], turnaround[i]);
    }

    printf("\nAverage Waiting Time = %.2f\n", total_wt / n);
    printf("Average Turnaround Time = %.2f\n", total_tat / n);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra rr.c -o rr
./rr

Sample Input and Output — Test 1

Enter number of processes: 3
Process 1 - Burst Time: 10
Process 2 - Burst Time: 5
Process 3 - Burst Time: 8
Enter time quantum: 2

PID	Burst	Waiting	Turnaround
1	10	13	23
2	5	10	15
3	8	13	21

Average Waiting Time = 12.00
Average Turnaround Time = 19.67

Sample Input and Output — Test 2 (larger quantum)

Enter number of processes: 4
Process 1 - Burst Time: 5
Process 2 - Burst Time: 3
Process 3 - Burst Time: 8
Process 4 - Burst Time: 6
Enter time quantum: 4

PID	Burst	Waiting	Turnaround
1	5	11	16
2	3	4	7
3	8	12	20
4	6	16	22

Average Waiting Time = 10.75
Average Turnaround Time = 16.25

Code Explanation

  • remaining[] — tracks how much burst time is left for each process. The outer do...while loop keeps cycling through all processes until every entry in remaining[] reaches zero.
  • The quantum checkif (remaining[i] > quantum) decides whether a process needs another round (it’s preempted, only quantum units run) or finishes this turn (it runs its last remaining[i] units and its waiting time is recorded).
  • waiting[i] = time − burst[i] — this only makes sense at the moment a process finishes, because by then time equals its completion time, and completion − burst = waiting (since all processes here arrive at time 0).
  • done flag — set to 1 at the start of each pass and flipped to 0 the moment any process still has remaining work; this is what lets the loop terminate exactly when the last process finishes.
  • Choosing the quantum matters — a very large quantum makes Round Robin behave like FCFS; a very small quantum increases fairness but adds more context-switch overhead in a real OS.

Time and Space Complexity

Step Time Space
Each full pass over n processes O(n) O(1) extra
Number of passes (worst case) O(max burst / quantum)
Overall O(n × max burst / quantum) O(n)

Round Robin vs FCFS

Property Round Robin FCFS
Preemptive Yes No
Response time Low, predictable Can be high (convoy effect)
Fairness Every process gets regular CPU turns Strict arrival order only
Overhead Context-switch cost per quantum None
Best for Time-sharing, interactive systems Batch processing

Related Programs

Recommended Books

Practice OS scheduling and C fundamentals with the C Programming Quiz App — 500+ MCQs covering scheduling, pointers, sorting, and more.
Download on Google Play →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>