Sub topic 14
Round Robin and Choosing the Time Quantum
Prerequisites and the Core Idea
Assume you know process states, the ready queue, context switches, and the formulas for waiting, turnaround, and response time.
Round Robin (RR) is a preemptive CPU scheduling algorithm. It gives each ready process a fixed time interval called the time quantum. When the quantum ends, the scheduler moves the running process to the back of the ready queue if it still has work.
This creates a cycle:
- Remove the process at the front of the queue.
- Run it for at most one quantum.
- If it finishes, record its completion time.
- Otherwise, place it at the back of the queue.
- Repeat until the queue is empty.
Worked Example
Suppose all processes arrive at time 0:
| Process | Burst time |
|---|---|
| P1 | 5 |
| P2 | 3 |
| P3 | 8 |
Use a quantum of 2. The Gantt chart is:
0 P1 2 P2 4 P3 6 P1 8 P2 9 P3 11 P1 12 P3 14 P3 16
The completion times are P1 = 12, P2 = 9, and P3 = 16.
For a process arriving at time 0:
- Turnaround time = completion time − arrival time
- Waiting time = turnaround time − burst time
- Response time = first start time − arrival time
| Process | Completion | Turnaround | Waiting | Response |
|---|---|---|---|---|
| P1 | 12 | 12 | 7 | 0 |
| P2 | 9 | 9 | 6 | 2 |
| P3 | 16 | 16 | 8 | 4 |
The following program simulates this example:
from collections import deque
def round_robin(bursts, quantum):
remaining = list(bursts)
queue = deque(range(len(bursts)))
finish = [0] * len(bursts)
time = 0
while queue:
i = queue.popleft()
run = min(quantum, remaining[i])
time += run
remaining[i] -= run
if remaining[i] == 0:
finish[i] = time
else:
queue.append(i)
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]Choosing the Time Quantum
A small quantum improves response time because every process gets CPU time sooner. However, it causes more preemptions and context switches. Context switching consumes CPU time without advancing a process's useful work.
A large quantum reduces context-switch overhead. If it is too large, RR behaves almost like First-Come First-Served and interactive processes may wait too long.
Exercise
Three processes arrive at time 0 with burst times P1 = 4, P2 = 2, and P3 = 3. With a quantum of 2, what are their completion times?
Show answer
The schedule is P1(0–2), P2(2–4), P3(4–6), P1(6–8), and P3(8–9). Completion times are P1 = 8, P2 = 4, and P3 = 9.