Skip to lesson

CPU scheduling: Round Robin, priority and multilevel queues

Round Robin is the favourite follow-up after FCFS and SJF: "Draw the chart for quantum 2, then tell me what happens when the quantum is huge." The interviewer is checking your queue rule at ties and whether you know the trade-offs.

Round Robin with a time quantum: worked example

Round Robin is a preemptive algorithm that keeps ready processes in a FIFO queue and lets each one run for at most one time quantum before going to the back.

The program below runs Round Robin with q = 2 on five processes (arrivals 0 to 4, bursts 5, 3, 1, 2 and 3) and prints the Gantt chart, finish times and waiting times.

Example 1 · Round Robin simulator (q = 2)

RoundRobinDemo.javaJava
import java.util.ArrayDeque;import java.util.Deque;
public class RoundRobinDemo {    public static void main(String[] args) {        String[] name = {"P1", "P2", "P3", "P4", "P5"};        int[] arrival = {0, 1, 2, 3, 4};        int[] burst = {5, 3, 1, 2, 3};        int q = 2, n = name.length;
        int[] left = burst.clone(), finish = new int[n];        Deque<Integer> ready = new ArrayDeque<>();        int time = 0, next = 0;        StringBuilder gantt = new StringBuilder("0");        while (next < n && arrival[next] <= time) ready.add(next++);        while (!ready.isEmpty() || next < n) {            if (ready.isEmpty()) {                    // CPU idle until the next arrival                time = arrival[next];                while (next < n && arrival[next] <= time) ready.add(next++);            }            int p = ready.poll();            int run = Math.min(q, left[p]);            time += run;            left[p] -= run;            gantt.append(" [").append(name[p]).append("] ").append(time);            while (next < n && arrival[next] <= time) ready.add(next++);   // arrivals first            if (left[p] > 0) ready.add(p);                                  // then the preempted one            else finish[p] = time;        }        System.out.println("Gantt: " + gantt);        int total = 0;        for (int i = 0; i < n; i++) {            int wt = finish[i] - arrival[i] - burst[i];            total += wt;            System.out.println(name[i] + ": finish " + finish[i] + ", WT " + wt);        }        System.out.printf("Average WT = %.2f%n", total / (double) n);    }}
Output
Gantt: 0 [P1] 2 [P2] 4 [P3] 5 [P1] 7 [P4] 9 [P5] 11 [P2] 12 [P1] 13 [P5] 14
P1: finish 13, WT 8
P2: finish 12, WT 8
P3: finish 5, WT 2
P4: finish 9, WT 4
P5: finish 14, WT 7
Average WT = 5.80

What this code does

  • At time 2, P2 and P3 have arrived; they join the queue before P1 goes back, so the order is P2, P3, P1. The comments mark this rule.
  • P3 needs only 1 ms, so it gives up the CPU after 1 ms instead of a full quantum.

If a process is not finished when its quantum ends, it is preempted and goes to the back of the queue. When a new process arrives at the same moment a quantum ends, the usual textbook convention puts the new arrival in the queue first; state this assumption in an exam.

  • A process that finishes early releases the CPU at once.
  • Waiting time = completion − arrival − burst, exactly as before.
  • With n ready processes, nobody waits more than (n − 1) × q for the next turn.
  • Round Robin has no starvation: every process gets a turn each round.

Revise: FCFS and SJF (opens in a new tab)

Find the bug

Find the wrong line

Each Round Robin working has one wrong line. Click it.

q = 4. P1 needs 6 ms and P2 needs 3 ms; both arrive at 0.

Round 1 of 2

Choosing the quantum: too small vs too large

Time quantum is the fixed maximum slice of CPU time a process gets in one turn under Round Robin, after which a timer interrupt hands the CPU to the next process.

The program below runs Round Robin on three jobs of 24, 3 and 3 ms, all arriving at 0, with quantum 1, 2, 4, 8 and 30, and prints the average waiting time and the context switches.

Example 2 · Effect of the quantum size

QuantumDemo.javaJava
import java.util.ArrayDeque;import java.util.Deque;
public class QuantumDemo {    // All jobs arrive at time 0. Returns {total waiting, context switches}.    static int[] simulate(int[] burst, int q) {        int n = burst.length, time = 0, switches = 0, last = -1, totalWait = 0;        int[] left = burst.clone();        Deque<Integer> ready = new ArrayDeque<>();        for (int i = 0; i < n; i++) ready.add(i);        while (!ready.isEmpty()) {            int p = ready.poll();            if (last != -1 && last != p) switches++;            last = p;            int run = Math.min(q, left[p]);            time += run;            left[p] -= run;            if (left[p] > 0) ready.add(p);            else totalWait += time - burst[p];        }        return new int[] {totalWait, switches};    }
    public static void main(String[] args) {        int[] burst = {24, 3, 3};        for (int q : new int[] {1, 2, 4, 8, 30}) {            int[] r = simulate(burst, q);            System.out.printf("q = %2d: avg WT = %5.2f, context switches = %d%n", q, r[0] / 3.0, r[1]);        }    }}
Output
q =  1: avg WT =  5.67, context switches = 9
q =  2: avg WT =  6.33, context switches = 6
q =  4: avg WT =  5.67, context switches = 3
q =  8: avg WT =  8.33, context switches = 3
q = 30: avg WT = 17.00, context switches = 2

What this code does

  • With q = 30 every job finishes in one turn, so RR equals FCFS (average wait 17). Small quanta help the short jobs but add many switches.
  • q = 4 is the classic textbook case: average wait 17 ÷ 3 = 5.67 with only 3 switches, a good balance for this data.

If q is very large, every process finishes within one quantum and Round Robin becomes FCFS. If q is very small, the CPU spends a large share of its time on context switches.

A common rule of thumb is to choose q so that about 80% of CPU bursts finish within one quantum, and to keep q much larger than the context-switch time. Real systems use roughly 10 to 100 ms.

Predict the output

Quantum trade-offs

Pick the output for each quantum choice.

Round 1 of 2

A 9 ms quantum and a 1 ms context switch. What percentage of time goes to switching?

double q = 9, sw = 1;
System.out.println(100 * sw / (q + sw));

Priority scheduling, starvation and aging

Priority scheduling gives the CPU to the ready process with the highest priority; many textbooks use a smaller number for a higher priority.

The program below runs non-preemptive priority scheduling on five jobs that arrive at time 0, then shows how aging improves the priority number of a process that keeps waiting.

Example 3 · Priority scheduling and aging

PriorityAgingDemo.javaJava
public class PriorityAgingDemo {    public static void main(String[] args) {        // Non-preemptive priority, all arrive at 0. Smaller number = higher priority.        String[] name = {"P1", "P2", "P3", "P4", "P5"};        int[] burst = {10, 1, 2, 1, 5};        int[] prio = {3, 1, 4, 5, 2};        boolean[] done = new boolean[5];        int time = 0, totalWait = 0;        StringBuilder gantt = new StringBuilder("0");        for (int k = 0; k < 5; k++) {            int pick = -1;            for (int i = 0; i < 5; i++) {                if (!done[i] && (pick == -1 || prio[i] < prio[pick])) pick = i;            }            totalWait += time;            time += burst[pick];            done[pick] = true;            gantt.append(" [").append(name[pick]).append("] ").append(time);        }        System.out.println("Gantt: " + gantt);        System.out.printf("Average WT = %.2f%n", totalWait / 5.0);
        // Aging: every 10 ms of waiting improves a priority number by 1.        int priority = 127;        for (int waited = 0; waited <= 40; waited += 10) {            System.out.println("Waited " + waited + " ms -> priority " + (priority - waited / 10));        }    }}
Output
Gantt: 0 [P2] 1 [P5] 6 [P1] 16 [P3] 18 [P4] 19
Average WT = 8.20
Waited 0 ms -> priority 127
Waited 10 ms -> priority 126
Waited 20 ms -> priority 125
Waited 30 ms -> priority 124
Waited 40 ms -> priority 123

What this code does

  • The order follows priority numbers 1, 2, 3, 4, 5: P2, P5, P1, P3, P4. Average wait is (0 + 1 + 6 + 16 + 18) ÷ 5 = 8.2.
  • The aging loop shows the idea: a process stuck at priority 127 improves steadily while it waits, so it cannot wait forever.

Always check the question: some systems treat a bigger number as higher priority. Priority scheduling can be preemptive (a higher-priority arrival takes the CPU) or non-preemptive, and SJF is the special case where priority = predicted burst.

Its danger is starvation: a low-priority process may never run if higher-priority work keeps arriving. Aging fixes this by slowly raising the priority of a process the longer it waits, so in the end it becomes the most important and runs.

Predict the output

Who runs, and how does aging help?

A smaller number means a higher priority.

Round 1 of 2

Three jobs are ready at time 0. Which one runs first?

String[] name = {"P1", "P2", "P3"};
int[] prio = {3, 1, 2};
int best = 0;
for (int i = 1; i < 3; i++) if (prio[i] < prio[best]) best = i;
System.out.println(name[best]);

Multilevel and multilevel feedback queues

MLFQ (multilevel feedback queue) uses several queues with different quanta and moves processes between them: CPU-heavy jobs sink to lower queues and long waiters can rise.

A multilevel queue splits the ready queue into separate queues, for example system, interactive and batch, each with its own algorithm. The design below and the program after it use three levels.

Text
Q0: RR, q = 8   (new jobs start here)Q1: RR, q = 16  (jobs that used all of q = 8)Q2: FCFS        (jobs that used all of q = 16)A lower queue runs only when all higher queues are empty.

Example 4 · A three-level feedback queue

MlfqDemo.javaJava
import java.util.ArrayDeque;import java.util.Deque;
public class MlfqDemo {    public static void main(String[] args) {        String[] name = {"A", "B", "C"};        // all arrive at time 0        int[] left = {30, 6, 20};        int[] quantum = {8, 16, Integer.MAX_VALUE};   // Q2 is FCFS        Deque<Integer>[] queues = new Deque[3];        for (int i = 0; i < 3; i++) queues[i] = new ArrayDeque<>();        for (int i = 0; i < 3; i++) queues[0].add(i);
        int time = 0;        for (int level = 0; level < 3; level++) {            while (!queues[level].isEmpty()) {                int p = queues[level].poll();                int run = Math.min(quantum[level], left[p]);                time += run;                left[p] -= run;                String what = left[p] == 0 ? "finished" : "moved to Q" + (level + 1);                System.out.println("Q" + level + ": " + name[p] + " ran " + run + " ms, t=" + time + ", " + what);                if (left[p] > 0) queues[level + 1].add(p);            }        }    }}
Output
Q0: A ran 8 ms, t=8, moved to Q1
Q0: B ran 6 ms, t=14, finished
Q0: C ran 8 ms, t=22, moved to Q1
Q1: A ran 16 ms, t=38, moved to Q2
Q1: C ran 12 ms, t=50, finished
Q2: A ran 6 ms, t=56, finished

What this code does

  • B (6 ms) finishes in its first quantum at the top level, at time 14. Short jobs get through fast without the OS knowing their length in advance.
  • A is CPU-heavy, uses every quantum and sinks to Q2. With no new arrivals the lower queue simply runs when the upper ones are empty; the unchecked-array warning from new Deque[3] is harmless here.

In a plain multilevel queue a process stays in its queue for life, and higher queues usually have absolute priority over lower ones. In an MLFQ new processes start at the top with a small quantum; a process that uses its whole quantum moves down, and one that waits too long may move up (aging).

Short and interactive jobs therefore finish quickly at the top, while CPU-heavy jobs sink to lower queues with longer quanta.

Order of execution

The life of a CPU-heavy job

Job A needs 30 ms. Put its journey through the three queues in order.

Interview questions with model answers

Scheduling questions mix definitions ("What is aging?"), comparisons ("RR vs FCFS") and small numericals done aloud. A strong answer gives the definition, one number or example, and the trade-off.

  • Must say: RR = FCFS + preemption by quantum; no starvation.
  • Must say: q too large gives FCFS; q too small gives switching overhead.
  • Must say: priority leads to starvation, and aging is the fix.
  • Must say: MLFQ moves CPU-heavy jobs down and can move waiting jobs up.

Find the bug

Weak spoken answers

Each spoken answer has one wrong sentence. Click it.

Answer to "Compare Round Robin and SJF."

Round 1 of 2

Next step

Check your understanding

12 quick questions