Sub topic 14
Round Robin and Choosing the Time Quantum
This chapter assumes you know what a ready queue is, how a context switch works, and how to compute waiting, turnaround and response time from a Gantt chart. It builds on FCFS and SJF/SRTF and adds the one parameter that matters here, the time quantum.
The idea
Round Robin (RR) is FCFS with preemption by a timer. Each process gets the CPU for at most one time quantum (also called a time slice), written q. If the process is still unfinished when the quantum ends, a timer interrupt fires. The kernel saves the process, moves it to the tail of the ready queue, and dispatches the process at the head.
A process leaves the CPU in one of three ways:
- Its burst finishes before the quantum ends. It terminates, and the next process starts immediately without waiting for the quantum to expire.
- It blocks for I/O before the quantum ends. It goes to a wait queue and returns to the ready queue later.
- The quantum expires. It is preempted and goes to the tail of the ready queue.
RR is preemptive and uses no knowledge of burst lengths. It cannot starve a process. With n processes in the ready queue, a process waits at most (n - 1) * q before its next turn. This gives a bounded response time, which is why RR suits interactive, time-sharing systems.
Worked example: all processes arrive at time 0
Take bursts P0 = 5, P1 = 3, P2 = 8, with q = 2.
| Interval | Process | Remaining after | Queue after |
|---|---|---|---|
| 0-2 | P0 | 3 | P1, P2, P0 |
| 2-4 | P1 | 1 | P2, P0, P1 |
| 4-6 | P2 | 6 | P0, P1, P2 |
| 6-8 | P0 | 1 | P1, P2, P0 |
| 8-9 | P1 | 0 (done) | P2, P0 |
| 9-11 | P2 | 4 | P0, P2 |
| 11-12 | P0 | 0 (done) | P2 |
| 12-16 | P2 | 0 (done) | empty |
P2 runs alone from 12 to 16. The scheduler may still take timer interrupts, but there is no other process to switch to. In a Gantt chart you can draw this as one block.
Finish times are P0 = 12, P1 = 9, P2 = 16.
- Turnaround time = finish - arrival, so 12, 9, 16. The average is 37/3 ≈ 12.33.
- Waiting time = turnaround - burst, so 7, 6, 8. The average is 7.
- Response time = first CPU time - arrival, so 0, 2, 4.
The simulation below reproduces these numbers.
from collections import deque
def round_robin(bursts, quantum):
"""All processes arrive at time 0. Returns finish times in input order."""
remaining = list(bursts)
queue = deque(range(len(bursts)))
finish = [0] * len(bursts)
t = 0
while queue:
i = queue.popleft()
run = min(quantum, remaining[i])
t += run
remaining[i] -= run
if remaining[i] > 0:
queue.append(i)
else:
finish[i] = t
return finish
bursts = [5, 3, 8]
finish = round_robin(bursts, 2)
waiting = [f - b for f, b in zip(finish, bursts)]
print(finish) # [12, 9, 16]
print(waiting) # [7, 6, 8]Arrival times and the tie rule
When processes arrive at different times, a tie can occur. A new process may arrive at the exact moment another process's quantum expires. The usual GATE convention is that the newly arrived process enters the queue before the preempted process is re-queued. Most textbooks and solution keys follow this. If a question states a different rule, follow the question.
The next simulation handles arrivals, the tie rule, and response time.
from collections import deque
def rr_with_arrivals(procs, q):
"""procs: list of (arrival, burst). Returns (finish, first_run) lists."""
n = len(procs)
order = sorted(range(n), key=lambda i: (procs[i][0], i))
remaining = [b for _, b in procs]
finish = [0] * n
first = [None] * n
queue = deque()
nxt = 0
t = 0
while True:
while nxt < n and procs[order[nxt]][0] <= t:
queue.append(order[nxt])
nxt += 1
if not queue:
if nxt == n:
break
t = procs[order[nxt]][0]
continue
p = queue.popleft()
if first[p] is None:
first[p] = t
run = min(q, remaining[p])
t += run
remaining[p] -= run
while nxt < n and procs[order[nxt]][0] <= t:
queue.append(order[nxt]) # arrivals first ...
nxt += 1
if remaining[p] > 0:
queue.append(p) # ... then the preempted process
else:
finish[p] = t
return finish, first
procs = [(0, 5), (1, 3), (2, 1), (3, 2)]
finish, first = rr_with_arrivals(procs, 2)
print(finish) # [11, 10, 5, 9]
print([f - a for f, (a, b) in zip(finish, procs)]) # turnaround [11, 9, 3, 6]
print([f - a - b for f, (a, b) in zip(finish, procs)]) # waiting [6, 6, 2, 4]
print([s - a for s, (a, b) in zip(first, procs)]) # response [0, 1, 2, 4]The code also handles an idle CPU: if the queue is empty, the clock jumps to the next arrival.
Choosing the time quantum
The quantum controls a trade-off between throughput and responsiveness.
| Quantum | Behavior | Cost |
|---|---|---|
| Very large (q ≥ longest burst) | Degenerates into FCFS | Convoy effect, poor response time |
| Moderate | Short bursts finish in one turn, long ones are sliced | Good balance |
| Very small | Approaches processor sharing, where each of n processes gets 1/n of the CPU | Context-switch overhead dominates |
Let s be the context-switch time. The fraction of CPU time spent on useful work is roughly q / (q + s). With q = 10 ms and s = 1 ms, that is 10/11 ≈ 91%. With q = 1 ms and s = 1 ms, it is only 50%. In GATE problems the switch cost is usually zero unless the question says otherwise. If it is given, add s to the clock at every switch.
The worst-case time before a process first runs is (n - 1) * q. If a system must respond within R, you need q ≤ R / (n - 1).
A common rule of thumb is to choose q so that about 80% of CPU bursts are shorter than q. Most processes then finish a burst without being preempted, and only the long CPU-bound ones get sliced.
A smaller quantum does not always help turnaround
Three processes each have a burst of 10 and all arrive at time 0.
- With q = 1, they finish at 28, 29 and 30. Average turnaround is 29.
- With q = 10 (equivalent to FCFS), they finish at 10, 20 and 30. Average turnaround is 20.
RR gives the best average turnaround only when the quantum is larger than most bursts. It usually gives longer average turnaround than SJF, but a better response time.
Comparison with other algorithms
| Property | FCFS | SJF/SRTF | Round Robin |
|---|---|---|---|
| Preemptive | No | SRTF only | Yes |
| Needs burst estimates | No | Yes | No |
| Starvation | No | Possible | No |
| Average waiting time | Can be high | Optimal | Usually higher than SJF |
| Response time | Poor | Variable | Good, bounded |
Common errors
- Forgetting to re-queue a preempted process behind newly arrived ones.
- Preempting a process that has already finished. A process that completes in the middle of a quantum releases the CPU at once.
- Computing waiting time as finish minus burst without subtracting arrival. The correct formula is finish - arrival - burst.
- Counting the first dispatch as a preemption when counting context switches. In a typical GATE question, count a switch at each change from one process to another. Check what the question asks.
- Treating response time as the same as waiting time. They are equal only for a process that runs once without being preempted.
Exercise
Three processes P0, P1, P2 arrive at time 0 with CPU bursts 6, 4 and 2. The scheduler uses Round Robin with q = 3 and zero context-switch cost. Find the Gantt chart, the average waiting time, and the response time of each process.
Show answer
Gantt chart: P0 0-3, P1 3-6, P2 6-8, P0 8-11, P1 11-12. Finish times are P0 = 11, P1 = 12, P2 = 8. Waiting time = finish - burst, so P0 = 5, P1 = 8, P2 = 6. The average waiting time is 19/3 ≈ 6.33. Response times are P0 = 0, P1 = 3, P2 = 6.
Exercise
Under Round Robin with n processes in the ready queue, a context switch cost of s and a quantum of q, what is the longest a process may wait before it first gets the CPU, if all n processes arrive at time 0 and each burst is at least q?
Show answer
The process at the back of the queue waits for the n - 1 processes ahead of it. Each one runs for q and is followed by a switch costing s. So the wait is (n - 1) (q + s), or (n - 1) q if the switch cost is zero.