You start a long video export and then try to type a message. Under first come, first served, your typing would wait until the export finished, which could take minutes. That is clearly wrong: the computer should feel responsive even while it is busy. But SJF cannot help either, because the kernel does not know how long anything will take.
The fix is to stop letting any process keep the CPU for as long as it likes.
Taking turns
Round robin (RR) keeps the ready processes in a queue, like FCFS, but each one may only run for a fixed amount of time, the time slice or quantum, before it must give up the CPU. When the slice is over, the timer interrupt (Lesson 1.2) hands control to the kernel, the process goes to the back of the queue, and the next one runs. Taking the CPU away from a process that has not finished is preemption.
Here are the same three jobs as in Lesson 3.1, P1 24 ms, P2 and P3 3 ms each, with a 4 ms slice. The bottom pane is FCFS, for comparison.
Round robin with a 4 ms slice
-
P1, P2 and P3 arrive
Three processes are ready at time 0. P1 is a long job; P2 and P3 are short.
- Time
- 0 ms
- Running
- idle
- Ready queue
- P1 → P2 → P3
- P1
- 24 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
P1 runs
The scheduler picks P1, because it is at the front of the ready queue, and it may run for up to 4 ms.
- Time
- 0 ms
- Running
- P1
- Ready queue
- P2 → P3
- P1
- 24 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
Slice over: P1 → P2
P1 has used its whole 4 ms slice. The timer interrupt gives the CPU back to the kernel, and P1 goes to the back of the ready queue. The scheduler picks P2, because it is at the front of the ready queue, and it may run for up to 4 ms. The kernel takes the CPU back. This is what makes round robin fair.
- Time
- 4 ms
- Running
- P2
- Ready queue
- P3 → P1
- P1
- 20 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
P2 finishes, P3 runs
P2 finishes at 7 ms. It waited 4 ms in all, and its turnaround (arrival to finish) was 7 ms. The scheduler picks P3, because it is at the front of the ready queue, and it may run for up to 4 ms.
- Time
- 7 ms
- Running
- P3
- Ready queue
- P1
- P1
- 20 of 24 ms left
- P2
- done at 7: waited 4, turnaround 7
- P3
- 3 of 3 ms left
-
P3 finishes, P1 runs
P3 finishes at 10 ms. It waited 7 ms in all, and its turnaround (arrival to finish) was 10 ms. The scheduler picks P1, because it is the only process ready.
- Time
- 10 ms
- Running
- P1
- Ready queue
- empty
- P1
- 20 of 24 ms left
- P2
- done at 7: waited 4, turnaround 7
- P3
- done at 10: waited 7, turnaround 10
-
P1 finishes
P1 finishes at 30 ms. It waited 6 ms in all, and its turnaround (arrival to finish) was 30 ms. Every process is done at 30 ms. The average wait is 5.67 ms.
- Time
- 30 ms
- Running
- idle
- Ready queue
- empty
- P1
- done at 30: waited 6, turnaround 30
- P2
- done at 7: waited 4, turnaround 7
- P3
- done at 10: waited 7, turnaround 10
- Average wait
- 5.67 ms
- Average turnaround
- 15.67 ms
- Average response
- 3.67 ms
P2 started at 4 ms and P3 at 7 ms, instead of waiting behind all of P1. The average wait is 5.67 ms against 17 ms for FCFS, and the average response time is 3.67 ms against 17 ms. Round robin did this without knowing any burst lengths: short jobs simply finish within their first slice and leave.
Look at the last stretch of the chart: once P2 and P3 are done, P1 is alone. Its slice still ends every 4 ms, but there is nobody else to run, so P1 just carries on, with no switch.
Who goes first when two things happen at once?
Sometimes a new process arrives at the very moment a slice ends. Both need a place in the queue. Textbooks do not all agree on the order, so this course uses one rule, and the simulator follows it: a process that arrives at the same moment a slice ends joins the queue before the process that was preempted. The newcomer has been waiting for its first chance, while the preempted process has just had a turn.
A new arrival and a slice end at the same moment
-
P1 arrives
P1 arrives and joins the ready queue. Ready queue: P1.
- Time
- 0 ms
- Running
- idle
- Ready queue
- P1
- P1
- 4 of 4 ms left
- P2
- arrives at 2
-
P1 runs
The scheduler picks P1, because it is the only process ready.
- Time
- 0 ms
- Running
- P1
- Ready queue
- empty
- P1
- 4 of 4 ms left
- P2
- arrives at 2
-
P2 arrives
P2 arrives and joins the ready queue. Ready queue: P2.
- Time
- 2 ms
- Running
- P1
- Ready queue
- P2
- P1
- 2 of 4 ms left
- P2
- 2 of 2 ms left
-
Slice over: P1 → P2
P1 has used its whole 2 ms slice. The timer interrupt gives the CPU back to the kernel, and P1 goes to the back of the ready queue. The scheduler picks P2, because it is at the front of the ready queue, and it may run for up to 2 ms.
- Time
- 2 ms
- Running
- P2
- Ready queue
- P1
- P1
- 2 of 4 ms left
- P2
- 2 of 2 ms left
-
P2 finishes, P1 runs
P2 finishes at 4 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 2 ms. The scheduler picks P1, because it is the only process ready.
- Time
- 4 ms
- Running
- P1
- Ready queue
- empty
- P1
- 2 of 4 ms left
- P2
- done at 4: waited 0, turnaround 2
-
P1 finishes
P1 finishes at 6 ms. It waited 2 ms in all, and its turnaround (arrival to finish) was 6 ms. Every process is done at 6 ms. The average wait is 1 ms.
- Time
- 6 ms
- Running
- idle
- Ready queue
- empty
- P1
- done at 6: waited 2, turnaround 6
- P2
- done at 4: waited 0, turnaround 2
- Average wait
- 1 ms
- Average turnaround
- 4 ms
- Average response
- 0 ms
When you work through a round-robin exercise elsewhere, check which rule it uses. The other common choice puts the preempted process first, and the chart comes out different.
Choosing the slice
The slice length decides everything about round robin.
- Too long, and round robin turns into FCFS. If the slice is longer than every burst, nobody is ever preempted. Press Try it on the first sim, set the slice to 24, and compare the two panes.
- Too short, and the CPU spends its time switching. Every switch saves and loads registers and disturbs the caches (Lesson 2.5), and no process makes progress while it happens.
The next sim makes the switching cost visible. Each time the CPU moves to a different process it now spends 1 ms switching, drawn as a dark grey block. The top pane uses a 1 ms slice, the bottom one a 10 ms slice.
The price of switching: a 1 ms slice against a 10 ms slice
-
P1, P2 and P3 arrive
P1, P2 and P3 arrive and join the ready queue. Ready queue: P1 → P2 → P3.
- Time
- 0 ms
- Running
- idle
- Ready queue
- P1 → P2 → P3
- P1
- 24 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
P1 runs
The scheduler picks P1, because it is at the front of the ready queue, and it may run for up to 1 ms.
- Time
- 0 ms
- Running
- P1
- Ready queue
- P2 → P3
- P1
- 24 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
Slice over: P1 → P2
P1 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P1 goes to the back of the ready queue. The scheduler picks P2, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P2 takes 1 ms, during which no process runs.
- Time
- 1 ms
- Running
- switching to P2 (until 2)
- Ready queue
- P3 → P1
- P1
- 23 of 24 ms left
- P2
- 3 of 3 ms left
- P3
- 3 of 3 ms left
-
Slice over: P2 → P3
P2 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P2 goes to the back of the ready queue. The scheduler picks P3, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P3 takes 1 ms, during which no process runs.
- Time
- 3 ms
- Running
- switching to P3 (until 4)
- Ready queue
- P1 → P2
- P1
- 23 of 24 ms left
- P2
- 2 of 3 ms left
- P3
- 3 of 3 ms left
-
Slice over: P3 → P1
P3 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P3 goes to the back of the ready queue. The scheduler picks P1, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P1 takes 1 ms, during which no process runs.
- Time
- 5 ms
- Running
- switching to P1 (until 6)
- Ready queue
- P2 → P3
- P1
- 23 of 24 ms left
- P2
- 2 of 3 ms left
- P3
- 2 of 3 ms left
-
Slice over: P1 → P2
P1 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P1 goes to the back of the ready queue. The scheduler picks P2, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P2 takes 1 ms, during which no process runs.
- Time
- 7 ms
- Running
- switching to P2 (until 8)
- Ready queue
- P3 → P1
- P1
- 22 of 24 ms left
- P2
- 2 of 3 ms left
- P3
- 2 of 3 ms left
-
Slice over: P2 → P3
P2 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P2 goes to the back of the ready queue. The scheduler picks P3, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P3 takes 1 ms, during which no process runs.
- Time
- 9 ms
- Running
- switching to P3 (until 10)
- Ready queue
- P1 → P2
- P1
- 22 of 24 ms left
- P2
- 1 of 3 ms left
- P3
- 2 of 3 ms left
-
Slice over: P3 → P1
P3 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P3 goes to the back of the ready queue. The scheduler picks P1, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P1 takes 1 ms, during which no process runs.
- Time
- 11 ms
- Running
- switching to P1 (until 12)
- Ready queue
- P2 → P3
- P1
- 22 of 24 ms left
- P2
- 1 of 3 ms left
- P3
- 1 of 3 ms left
-
Slice over: P1 → P2
P1 has used its whole 1 ms slice. The timer interrupt gives the CPU back to the kernel, and P1 goes to the back of the ready queue. The scheduler picks P2, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P2 takes 1 ms, during which no process runs.
- Time
- 13 ms
- Running
- switching to P2 (until 14)
- Ready queue
- P3 → P1
- P1
- 21 of 24 ms left
- P2
- 1 of 3 ms left
- P3
- 1 of 3 ms left
-
P2 finishes, P3 runs
P2 finishes at 15 ms. It waited 12 ms in all, and its turnaround (arrival to finish) was 15 ms. The scheduler picks P3, because it is at the front of the ready queue, and it may run for up to 1 ms. Switching to P3 takes 1 ms, during which no process runs.
- Time
- 15 ms
- Running
- switching to P3 (until 16)
- Ready queue
- P1
- P1
- 21 of 24 ms left
- P2
- done at 15: waited 12, turnaround 15
- P3
- 1 of 3 ms left
-
P3 finishes, P1 runs
P3 finishes at 17 ms. It waited 14 ms in all, and its turnaround (arrival to finish) was 17 ms. The scheduler picks P1, because it is the only process ready. Switching to P1 takes 1 ms, during which no process runs.
- Time
- 17 ms
- Running
- switching to P1 (until 18)
- Ready queue
- empty
- P1
- 21 of 24 ms left
- P2
- done at 15: waited 12, turnaround 15
- P3
- done at 17: waited 14, turnaround 17
-
P1 finishes
P1 finishes at 39 ms. It waited 15 ms in all, and its turnaround (arrival to finish) was 39 ms. Every process is done at 39 ms. The average wait is 13.67 ms.
- Time
- 39 ms
- Running
- idle
- Ready queue
- empty
- P1
- done at 39: waited 15, turnaround 39
- P2
- done at 15: waited 12, turnaround 15
- P3
- done at 17: waited 14, turnaround 17
- Average wait
- 13.67 ms
- Average turnaround
- 23.67 ms
- Average response
- 2 ms
With a 1 ms slice there were 9 switches, and the jobs did not all finish until 39 ms. With a 10 ms slice there were only 3 switches, and everything was done by 33 ms. But the tiny slice did give the best response: on average a job first ran after 2 ms, against 8.67 ms with the long slice.
So the slice is a trade between response time and overhead. Real systems choose a slice that is long compared with the cost of one switch, so that the overhead stays small, but short enough that an interactive program never waits long for its turn.
Round robin against SJF
On average waiting time, round robin usually loses to SJF: it keeps interrupting jobs that would have finished sooner if left alone. What it wins is predictability. No process waits more than a bounded time for its next turn, no process can hog the CPU, and the scheduler needs no knowledge of the future. That is why some form of time slicing is in every general-purpose operating system.
In the real world
Linux does not give ordinary programs one fixed slice: its scheduler sizes each task’s turn from how many tasks are runnable and their priorities (Lesson 3.5). But it does offer a classic round robin for time-critical programs:
$ cat /proc/sys/kernel/sched_rr_timeslice_ms
$ chrt -r 10 ./my_program
The first file holds the slice of the real-time SCHED_RR policy, and chrt -r starts a program under that policy (it needs root). Ordinary programs run under the default policy instead. :::
Recap
- Round robin gives each ready process a time slice; when it runs out, the timer interrupt preempts it and it goes to the back of the queue.
- Short jobs finish in their first slice, so response time is much better than with FCFS.
- In this course, a process that arrives just as a slice ends queues before the preempted one.
- A slice that is too long turns RR into FCFS; one that is too short wastes the CPU on context switches.
- RR usually has a worse average wait than SJF, but it needs no knowledge of burst lengths and never lets a process hog the CPU.
Quick check
Quick check
-
Preemption puts it back in line. It will get another slice when its turn comes round again.
-
Each switch has a fixed cost. The smaller the slice, the more switches per second, and the more time is lost.
-
SJF is optimal for average waiting time. Round robin trades some of that for fairness and fast response without knowing the future.
-
The newcomer joins the back of the queue first, then the preempted process joins behind it.
Join the discussion