5.6More Queues and Applications
Round-robin: taking turns
An operating system makes many programs look as if they run at once on a single processor. The best-known way is round-robin scheduling: each process runs for a short time in turn. A queue keeps the order. A process whose time slice is over but whose work is not done goes back to the rear of the queue.
- How round-robin is built with a queue
- How to trace a run with quantum 2 in a table
- How the slice length affects waiting time
1Idea
Each process has a run time. The scheduler picks a time slice (the quantum, quantum here). The loop works like this:
- The process at the front of the queue is taken with dequeue.
- The process runs for at most
quantum. If less time is left, it runs only for what is left. - If it is not done, it goes back to the rear with enqueue. If it is done, its finish time is recorded.
- When the queue is empty, every process has finished.
The queue is what makes this fair. A process that goes back to the rear does not get another turn until everyone waiting before it has run one slice. No process waits forever.
A trace with quantum 2
There are three processes: A takes 5 units, B 2, C 4. All of them are in the queue at time 0, in the order A, B, C.
| Time | Runs | What happened | Queue after |
|---|---|---|---|
| 0 → 2 | A | 3 units left, back to the rear | B, C, A |
| 2 → 4 | B | done (t = 4) | C, A |
| 4 → 6 | C | 2 units left, back to the rear | A, C |
| 6 → 8 | A | 1 unit left, back to the rear | C, A |
| 8 → 10 | C | done (t = 10) | A |
| 10 → 11 | A | ran 1 unit, done (t = 11) | empty |
The finish times are A 11, B 4, C 10. The short job B finished at 4 without waiting for A. Running them one after another, B would only finish at 7.
The C code builds the queue itself on a circular array. An array as long as the number of processes is enough: a process leaves the queue before it goes back in, so the queue never holds more than n processes at once. Java uses java.util.ArrayDeque and Python uses collections.deque.
If the slice is very small, the number of switches between processes grows, and every switch has a cost. If the slice is very large, round-robin turns into running jobs one after another, and short jobs wait behind long ones. Real systems pick a value in between.
2Step by step
Three processes: A 5, B 2, C 4 units. The quantum is 2. The top row is the queue; the bottom row holds finished processes. The time left is under each cell.
- Start. The three processes wait in order: A, B, C.
timeis 0. - A is taken and runs for 2 units. 3 units are left, so it goes back to the rear. The queue is B, C, A.
timeis 2. - B is taken and runs for 2 units. It is done: finish time 4. It does not go back. The queue is C, A.
- C is taken and runs for 2 units. 2 units are left; it goes back to the rear. The queue is A, C.
timeis 6. - A runs for 2 units again. 1 unit is left; it goes back to the rear. The queue is C, A.
timeis 8. - C runs for 2 units and is done: finish time 10. Only A is left in the queue.
- A has 1 unit left, so it does not use the whole slice. It runs for 1 unit and finishes at 11. The queue is empty and the loop stops.
void roundRobin(int burst[], int n, int quantum, int finish[]) { int remaining[n]; int queue[n]; // circular queue of process numbers int front = 0, count = 0; for (int i = 0; i < n; i++) { remaining[i] = burst[i]; queue[(front + count) % n] = i; // enqueue count++; } int time = 0; while (count > 0) { int p = queue[front]; // dequeue front = (front + 1) % n; count--; int run = remaining[p] < quantum ? remaining[p] : quantum; time += run; remaining[p] -= run; if (remaining[p] > 0) { queue[(front + count) % n] = p; // not done: back to the rear count++; } else { finish[p] = time; // done } }}int[] roundRobin(int[] burst, int quantum) { int n = burst.length; int[] remaining = burst.clone(); int[] finish = new int[n]; java.util.ArrayDeque<Integer> queue = new java.util.ArrayDeque<>(); for (int i = 0; i < n; i++) queue.addLast(i); // every process waits in order int time = 0; while (!queue.isEmpty()) { int p = queue.pollFirst(); // dequeue int run = Math.min(remaining[p], quantum); time += run; remaining[p] -= run; if (remaining[p] > 0) queue.addLast(p); // not done: back to the rear else finish[p] = time; // done } return finish;}from collections import deque def round_robin(burst, quantum): remaining = list(burst) finish = [0] * len(burst) queue = deque(range(len(burst))) # every process waits in order time = 0 while queue: p = queue.popleft() # dequeue run = min(remaining[p], quantum) time += run remaining[p] -= run if remaining[p] > 0: queue.append(p) # not done: back to the rear else: finish[p] = time # done return finishThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Let T be the total run time and q the quantum. Each dequeue moves time forward by at least one unit, usually by q. There are at most T/q + n dequeues, and queue operations are O(1), so the time is O(T/q + n).
The queue and the array of remaining times take room for n processes.
Real operating systems also give processes priorities. Then they keep one queue per priority instead of a single queue, or use a priority queue.
4Common mistake
A common mistake is running the process for a whole slice every time:
time += quantum # even if less time is leftremaining[p] -= quantumIn A's last turn only 1 unit was left. With this code time becomes 12 and remaining[p] drops to -1. The right way is to run for min(remaining[p], quantum). Another mistake is putting an unfinished process back at the front of the queue: then no one else runs until it is done.
5Try it
The quantum is 3. Processes P takes 4 units and R 2; the order is P, R. When does P finish?
The answer is C. P runs from 0 to 3, has 1 unit left and goes to the rear. R runs from 3 to 5 and finishes. P runs from 5 to 6 and finishes at 6.
Show the answer
The answer is C. P runs from 0 to 3, has 1 unit left and goes to the rear. R runs from 3 to 5 and finishes. P runs from 5 to 6 and finishes at 6.
Where does a process go when its slice is over but its work is not done?
The answer is B. The process goes to the rear with enqueue, so it does not get another turn before everyone in front of it has run one slice.
Show the answer
The answer is B. The process goes to the rear with enqueue, so it does not get another turn before everyone in front of it has run one slice.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.