Sub topic 16
Multilevel Queue and Multilevel Feedback Queue Scheduling
This chapter assumes you know the ready queue, preemption, and the Round Robin, FCFS and priority algorithms from the earlier chapters. You should also be able to compute waiting, turnaround and response time from a Gantt chart.
Why one queue is not enough
A single ready queue applies one policy to every process. Real systems run different kinds of work together:
- Interactive programs need a fast response and use the CPU in short bursts.
- Batch jobs need throughput and can wait.
- System processes may need guaranteed attention.
Multilevel scheduling splits the ready queue into several queues, each with its own algorithm. Two questions then decide the design:
- How does a process get assigned to a queue?
- How does the scheduler pick between queues?
The two schemes in this chapter answer these questions differently.
Multilevel queue (MLQ) scheduling
In a multilevel queue, the assignment is fixed. A process is placed in one queue when it arrives and stays there until it terminates. The assignment depends on a property of the process, such as its type, memory size or priority.
A typical layout is:
| Queue | Process type | Algorithm |
|---|---|---|
| Q0 | System processes | Priority or FCFS |
| Q1 | Interactive (foreground) | Round Robin |
| Q2 | Batch (background) | FCFS |
Scheduling between the queues usually follows one of two rules:
- Fixed-priority preemptive. A lower queue runs only when every higher queue is empty. If a process arrives in Q0 while a Q2 process is running, the Q2 process is preempted at once.
- Time slicing between queues. Each queue receives a share of CPU time. For example, the foreground queue gets 80% and the background queue gets 20%.
MLQ has low scheduling overhead because deciding where a process goes costs almost nothing. It has two weaknesses:
- Starvation. Under fixed priority, a steady stream of foreground work can block the background queue forever.
- Inflexibility. A process that changes behavior cannot move. A CPU-hungry job stuck in the interactive queue keeps taking its quantum.
Multilevel feedback queue (MLFQ) scheduling
Multilevel feedback queue scheduling lets processes move between queues based on observed behavior. The scheduler does not need to know burst lengths in advance. It infers them from how a process uses its quanta.
A classic rule set is:
- All new processes enter the top queue, Q0.
- Each queue uses Round Robin, with a quantum that grows at lower levels (for example 4, 8, then FCFS at the bottom).
- If a process uses its full quantum without finishing, it is demoted to the next lower queue.
- If a process blocks for I/O before its quantum ends, it stays at its level (or is requeued there).
- A lower queue runs only when all higher queues are empty. A newly arrived process in a higher queue preempts the running lower-queue process.
- Optionally, a process that waits too long in a low queue is promoted. This is aging.
The effect is that short and I/O-bound jobs finish in the top queues, which have small quanta and the highest priority. Long CPU-bound jobs sink to the bottom, where they get big quanta and few context switches. MLFQ therefore approximates SJF without knowing burst times.
The parameters that define an MLFQ
An MLFQ is fully specified only when you fix all of the following. GATE questions state them in the problem text.
- The number of queues.
- The scheduling algorithm in each queue.
- The rule that decides when a process is demoted.
- The rule that decides when a process is promoted.
- The rule that decides which queue a new process enters.
If a question leaves one out, the standard defaults are: new processes enter the top queue, and a process is demoted when it uses its full quantum.
Worked example
Three processes arrive at time 0 with CPU bursts P1 = 10, P2 = 3, P3 = 6. The queues are:
- Q0: Round Robin, quantum 4.
- Q1: Round Robin, quantum 8.
- Q2: FCFS.
The schedule is built step by step:
- P1 runs 0 to 4. It has 6 left, so it is demoted to Q1.
- P2 runs 4 to 7. It needs only 3, so it finishes inside its quantum.
- P3 runs 7 to 11. It has 2 left, so it is demoted to Q1.
- Q0 is empty, so Q1 runs. P1 runs 11 to 17 and finishes, because 6 is less than the quantum of 8.
- P3 runs 17 to 19 and finishes.
The Gantt chart is P1 [0–4], P2 [4–7], P3 [7–11], P1 [11–17], P3 [17–19].
| Process | Burst | Finish | Turnaround (finish − arrival) | Waiting (turnaround − burst) | Response (first run − arrival) |
|---|---|---|---|---|---|
| P1 | 10 | 17 | 17 | 7 | 0 |
| P2 | 3 | 7 | 7 | 4 | 4 |
| P3 | 6 | 19 | 19 | 13 | 7 |
The average waiting time is (7 + 4 + 13) / 3 = 8.
Simulating it
The program below reproduces the table. It assumes all processes arrive at time 0, so it never needs to handle a mid-run preemption by an arriving process. The last queue is FCFS, so it runs a process to completion.
from collections import deque
def mlfq(bursts, quanta):
"""All processes arrive at time 0.
quanta gives the quantum of each Round Robin level; one extra FCFS level is appended.
Returns (finish, first_run) lists in input order."""
n = len(bursts)
queues = [deque() for _ in range(len(quanta) + 1)]
remaining = list(bursts)
for i in range(n):
queues[0].append(i)
finish = [0] * n
first = [None] * n
t = 0
while any(queues):
level = next(l for l, q in enumerate(queues) if q)
i = queues[level].popleft()
if first[i] is None:
first[i] = t
q = quanta[level] if level < len(quanta) else remaining[i]
run = min(q, remaining[i])
t += run
remaining[i] -= run
if remaining[i] == 0:
finish[i] = t
else:
queues[min(level + 1, len(queues) - 1)].append(i)
return finish, first
bursts = [10, 3, 6]
finish, first = mlfq(bursts, [4, 8])
waiting = [f - b for f, b in zip(finish, bursts)]
print(finish) # [17, 7, 19]
print(waiting) # [7, 4, 13]
print(first) # [0, 4, 7]Common errors and gotchas
- Treating MLQ and MLFQ as the same. In MLQ a process never changes queue. If a question says processes "move" or "are demoted", it describes MLFQ.
- Forgetting preemption between levels. A process arriving in a higher queue stops the running lower-queue process. Its remaining time is kept, and it returns to the same queue unless the problem says otherwise.
- Wrong demotion trigger. A process that finishes or blocks before its quantum expires is not demoted. Only a full, unfinished quantum causes demotion.
- Starvation in both schemes. Fixed-priority MLQ can starve lower queues. MLFQ can starve long jobs when short jobs keep arriving. Aging, a periodic promotion of waiting processes, is the standard remedy.
- Gaming the scheduler. A process can issue a short I/O request just before its quantum ends and so stay in a high queue forever. Many designs count total CPU time used at a level, instead of a single quantum, to close this gap.
- Ignoring context-switch cost. Smaller top-level quanta improve response time but increase switches. Questions that say "ignore context-switch time" are telling you to skip this.
Comparing the two schemes
| Property | Multilevel queue | Multilevel feedback queue |
|---|---|---|
| Queue assignment | Fixed at arrival | Changes with behavior |
| Needs burst-time knowledge | No | No, it infers it |
| Overhead | Low | Higher |
| Flexibility | Low | High |
| Starvation risk | Yes, without time slicing | Yes, without aging |
| Parameters to tune | Few | Many |
Exercise
Processes P1 (burst 6) and P2 (burst 2) both arrive at time 0. The system has Q0 (Round Robin, quantum 3) and Q1 (FCFS). P1 enters the queue first. Give the finish time of each process and the average waiting time.
Show answer
P1 runs 0 to 3 and has 3 left, so it is demoted to Q1. P2 runs 3 to 5 and finishes inside its quantum. Q0 is now empty, so P1 runs 5 to 8 and finishes. Finish times are P1 = 8 and P2 = 5. Waiting times are P1 = 8 − 6 = 2 and P2 = 5 − 2 = 3. The average waiting time is 2.5.
Exercise
A system uses a fixed-priority multilevel queue with a foreground Round Robin queue and a background FCFS queue. Foreground jobs keep arriving so that the foreground queue is never empty. What happens to a background job, and name two ways to fix it?
Show answer
The background job starves, because it runs only when the foreground queue is empty. One fix is time slicing between queues, such as 80% of CPU time for the foreground and 20% for the background. Another is to switch to a feedback queue with aging, so a long-waiting process is promoted.