Round Robin Scheduling
Give every ready process the CPU for one fixed time slice in turn. No process waits for long, which is why time-sharing systems are built on it — and the size of that slice decides everything else.
Taking Turns on One CPU
- Type – preemptive CPU scheduling algorithm.
- Data structure – one FIFO ready queue, treated as circular.
- Time quantum (q) – the longest a process may run before it is preempted, typically 10–100 ms.
- Rule – run the process at the front for min(q, remaining burst), then move to the next.
- Goal – fair sharing and short response time for interactive systems.
Round robin (RR) scheduling is the CPU scheduling algorithm designed for time-sharing systems. The scheduler keeps the ready processes in a queue and gives each one a fixed slice of CPU time, called the time quantum or time slice. When the slice ends, a timer interrupt preempts the process and the next one in the queue runs.
It can be described as FCFS with preemption. Processes are still served in arrival order, but none of them can hold the CPU for longer than one quantum at a time. The name comes from the idea of taking turns in a circle — every process gets a turn before anyone gets a second one.
- No starvation: with n ready processes, none waits more than (n − 1) × q for its next turn
- Equal turns, not equal CPU time: a process that blocks early gives the rest of its slice away
The Queue Rules, Including Ties
- Dispatch – take the process at the front of the ready queue and set a timer for q.
- Finish early – if its burst ends before the timer, it leaves; the next process starts at once.
- Quantum expires – otherwise the timer interrupts, the kernel saves its context, and it goes to the tail of the queue.
- Arrivals – new processes join the tail when they arrive.
- Repeat – until the queue is empty; if the queue is empty and processes are still to come, the CPU idles.
The quantum is a maximum, not a fixed length. A process that needs 1 unit with q = 2 runs for 1 unit and releases the CPU immediately; nothing waits for the rest of the slice.
One convention decides many exam answers: when a new process arrives at the same instant another is preempted, the new arrival is placed in the queue first, then the preempted process. Both orders appear in textbooks, so state the one you use. In the tracer above, P5 arrives at t = 4 exactly when P2's quantum expires, and P5 goes ahead of P2.
If only one process is ready, its quantum still expires, but the scheduler finds the queue empty and lets it continue. In the worked example, P1 runs from 10 to 30 in five consecutive slices.
- A process that blocks for I/O leaves the queue and rejoins the tail when the I/O completes
- If the queue is empty and more processes are due later, the CPU idles until the next arrival
Choosing the Time Quantum
The quantum is the only parameter, and it pulls two ways. Too small, and the CPU spends a large share of its time on context switches: with a switch cost of s and a quantum of q, at most q / (q + s) of the time does useful work — q = 1 ms with s = 0.1 ms wastes about 9%. Too large, and a process rarely gets preempted, so round robin behaves exactly like FCFS, with its long waits behind long jobs.
The usual rule of thumb is that about 80% of CPU bursts should be shorter than q. Then most processes finish in one slice, while long-running ones are still preempted. Modern systems use quanta of roughly 10–100 ms, against switch times of a few microseconds.
Average turnaround time does not simply improve as q shrinks. It is best when most processes finish within one quantum; making q smaller than that only adds switches.
- q → ∞ gives FCFS; q → 0 approaches processor sharing, where n processes each seem to run at 1/n speed
- Linux's CFS has no fixed quantum: it divides a target latency among the runnable tasks
Terms, operations, and practical uses
Terms to know
- Time quantumThe maximum CPU time a process gets per turn; also called a time slice.
- PreemptionTaking the CPU from a running process, here by a timer interrupt.
- Context switchSaving one process's state and loading another's; the overhead each slice pays.
- Time-sharingMany users or programs sharing one CPU, each seeing steady progress.
Related lessons
- CPU scheduling algorithmsFCFS, SJF, priority and round robin side by side.
- Context switchingWhat the kernel does at every quantum boundary.
- Priority schedulingChoose by importance instead of by turn.
- Multilevel feedback queueRound robin queues with different quanta and priorities.
Code in Python, C++ and Java
from collections import deque
def round_robin(processes, quantum):
# processes: (name, arrival, burst) tuples, sorted by arrival time.
remaining = {name: burst for name, _, burst in processes}
finish, gantt = {}, []
queue, clock, nxt = deque(), 0, 0
def admit(until):
nonlocal nxt
while nxt < len(processes) and processes[nxt][1] <= until:
queue.append(processes[nxt][0])
nxt += 1
admit(clock)
while queue or nxt < len(processes):
if not queue: # CPU idle until the next arrival
clock = processes[nxt][1]
admit(clock)
name = queue.popleft()
run = min(quantum, remaining[name])
gantt.append((name, clock, clock + run))
clock += run
remaining[name] -= run
# Arrivals during this slice join the queue before the preempted process.
admit(clock)
if remaining[name]:
queue.append(name)
else:
finish[name] = clock
return gantt, finish
procs = [("P1", 0, 5), ("P2", 1, 3), ("P3", 2, 1), ("P4", 3, 2), ("P5", 4, 3)]
gantt, finish = round_robin(procs, quantum=2)
print("Gantt:", " ".join(f"{n}[{s}-{e}]" for n, s, e in gantt))
total_tat = total_wt = 0
for name, arrival, burst in procs:
tat = finish[name] - arrival
wt = tat - burst
total_tat += tat
total_wt += wt
print(f"{name}: completion {finish[name]:>2} turnaround {tat:>2} waiting {wt:>2}")
print(f"average turnaround {total_tat / len(procs):.2f}, average waiting {total_wt / len(procs):.2f}")#include <algorithm>
#include <cstdio>
#include <deque>
#include <string>
#include <vector>
using namespace std;
struct Process {
string name;
int arrival, burst;
int remaining = 0, finish = 0;
};
int main() {
const int quantum = 2;
vector<Process> procs = {{"P1", 0, 5}, {"P2", 1, 3}, {"P3", 2, 1}, {"P4", 3, 2}, {"P5", 4, 3}};
for (Process& p : procs) p.remaining = p.burst;
deque<int> queue;
size_t next = 0;
int clock = 0;
auto admit = [&](int until) {
while (next < procs.size() && procs[next].arrival <= until) queue.push_back(next++);
};
admit(clock);
printf("Gantt:");
while (!queue.empty() || next < procs.size()) {
if (queue.empty()) { // CPU idle until the next arrival
clock = procs[next].arrival;
admit(clock);
}
int i = queue.front();
queue.pop_front();
int run = min(quantum, procs[i].remaining);
printf(" %s[%d-%d]", procs[i].name.c_str(), clock, clock + run);
clock += run;
procs[i].remaining -= run;
// Arrivals during this slice join the queue before the preempted process.
admit(clock);
if (procs[i].remaining > 0) {
queue.push_back(i);
} else {
procs[i].finish = clock;
}
}
printf("\n");
double totalTat = 0, totalWt = 0;
for (const Process& p : procs) {
int tat = p.finish - p.arrival;
int wt = tat - p.burst;
totalTat += tat;
totalWt += wt;
printf("%s: completion %2d turnaround %2d waiting %2d\n", p.name.c_str(), p.finish, tat, wt);
}
printf("average turnaround %.2f, average waiting %.2f\n", totalTat / procs.size(), totalWt / procs.size());
return 0;
}import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
static class Process {
final String name;
final int arrival, burst;
int remaining, finish;
Process(String name, int arrival, int burst) {
this.name = name;
this.arrival = arrival;
this.burst = burst;
this.remaining = burst;
}
}
public static void main(String[] args) {
int quantum = 2;
Process[] procs = {
new Process("P1", 0, 5), new Process("P2", 1, 3), new Process("P3", 2, 1),
new Process("P4", 3, 2), new Process("P5", 4, 3)
};
Deque<Process> queue = new ArrayDeque<>();
int next = 0, clock = 0;
while (next < procs.length && procs[next].arrival <= clock) queue.add(procs[next++]);
StringBuilder gantt = new StringBuilder("Gantt:");
while (!queue.isEmpty() || next < procs.length) {
if (queue.isEmpty()) { // CPU idle until the next arrival
clock = procs[next].arrival;
while (next < procs.length && procs[next].arrival <= clock) queue.add(procs[next++]);
}
Process p = queue.poll();
int run = Math.min(quantum, p.remaining);
gantt.append(String.format(" %s[%d-%d]", p.name, clock, clock + run));
clock += run;
p.remaining -= run;
// Arrivals during this slice join the queue before the preempted process.
while (next < procs.length && procs[next].arrival <= clock) queue.add(procs[next++]);
if (p.remaining > 0) {
queue.add(p);
} else {
p.finish = clock;
}
}
System.out.println(gantt);
double totalTat = 0, totalWt = 0;
for (Process p : procs) {
int tat = p.finish - p.arrival;
int wt = tat - p.burst;
totalTat += tat;
totalWt += wt;
System.out.printf("%s: completion %2d turnaround %2d waiting %2d%n", p.name, p.finish, tat, wt);
}
System.out.printf("average turnaround %.2f, average waiting %.2f%n",
totalTat / procs.length, totalWt / procs.length);
}
}Step through it
Running on P1(0,5) P2(1,3) P3(2,1) P4(3,2) P5(4,3) as (arrival, burst); q = 2
A Solved Example: One Long Job, Two Short Ones
- Given – P1 = 24, P2 = 3, P3 = 3 (burst times in ms), all arriving at t = 0, q = 4.
- Gantt chart – P1 0–4, P2 4–7, P3 7–10, then P1 alone from 10 to 30.
- Completion – P1 = 30, P2 = 7, P3 = 10.
- Averages – waiting 17 / 3 = 5.67 ms; turnaround 47 / 3 = 15.67 ms; response 11 / 3 = 3.67 ms.
Draw the Gantt chart first; every metric is read from it. P1 runs its first quantum and is preempted with 20 left. P2 and P3 each need less than q, so they finish inside their first slice at 7 and 10. After that only P1 is left, and it runs to completion.
Compare with FCFS on the same input: P1 would run first for 24 ms, and P2 and P3 would wait 24 and 27 ms — an average of 17 ms. Taking turns cuts that to 5.67 ms because the two short jobs no longer sit behind the long one. SJF would do even better (3 ms), but it needs to know the burst lengths in advance.
| Process | Arrival | Burst | Completion | Turnaround | Waiting | Response |
|---|---|---|---|---|---|---|
| P1 | 0 | 24 | 30 | 30 | 6 | 0 |
| P2 | 0 | 3 | 7 | 7 | 4 | 4 |
| P3 | 0 | 3 | 10 | 10 | 7 | 7 |
| Average | 15.67 | 5.67 | 3.67 |
- P1's waiting time counts every gap between its slices: 4 to 10 here, so 6
- With q = 3 the short jobs still finish in one slice, and the average wait drops to 15 / 3 = 5 ms
Round Robin With Arrival Times
- Queue the arrivals – put every process that has arrived by t = 0 in the queue, in arrival order.
- Run one slice – the front process runs for min(q, remaining burst).
- Add new arrivals – every process that arrived during the slice joins the tail.
- Then the preempted process – if the slice ended unfinished, it joins the tail after them.
- Repeat – write down the queue after every slice; that list is what examiners check.
Most exam questions give different arrival times, and nearly every wrong answer comes from the queue order, not the arithmetic. Keep the queue written out after each slice instead of working it out in your head.
The tracer runs P1(0, 5), P2(1, 3), P3(2, 1), P4(3, 2), P5(4, 3) with q = 2. The deciding moment is t = 4: P2's slice ends just as P5 arrives, so the queue becomes P3, P1, P4, P5, P2, with P5 ahead of P2. The run ends at t = 14 with an average turnaround of 8.6 and an average wait of 5.8.
- A process arriving exactly when a slice ends goes in before the preempted one (state this rule)
- A process that finishes inside its slice is never re-queued
Waiting, Turnaround and Response Time
Every scheduling question uses the same four definitions. Its strength shows up in response time — the delay before a process first runs — which is bounded by (n − 1) × q for n processes. Its weakness shows up in turnaround time: every process, even a short one, has its completion pushed back by the slices of everyone ahead of it.
- Completion time (CT): when the process's last slice ends
- Turnaround time = CT − arrival time
- Waiting time = turnaround time − burst time
- Response time = first time on the CPU − arrival time
- Context-switch time is ignored in exam answers unless the question gives it
Strengths and Weaknesses
RR trades average waiting time for fairness and predictability. It is the right choice when many users or tasks share one CPU and all of them need to stay responsive, and the wrong one when throughput on known batch jobs is all that matters. Real schedulers keep its time slice but add priorities and feedback, as in the multilevel feedback queue.
| FCFS | SJF | Priority | Round robin | |
|---|---|---|---|---|
| Preemptive | No | No (SRTF: yes) | Either | Yes |
| Needs burst lengths | No | Yes | No | No |
| Starvation | No | Yes | Yes, without aging | No |
| Average waiting time | Often high | Minimum | Depends on priorities | Medium |
| Response time | Poor | Poor for long jobs | Good for high priority | Good for all |
- Strength: no starvation and a bounded response time
- Strength: needs no knowledge of burst lengths
- Weakness: context-switch overhead on every slice
- Weakness: higher average waiting time than SJF, worst when all bursts are equal and long