G27
Self-checkedTier 2 14 min reados.scheduling-multilevel-realtime

Multilevel queues and real-time scheduling

Scheduling beyond FCFS, SJF and round robin: multilevel queues and the multilevel feedback queue (how a scheduler approximates shortest-job-first without knowing the bursts), periodic real-time tasks with rate-monotonic and earliest-deadline-first scheduling, the utilisation tests (U at most n(2^(1/n) - 1) for rate monotonic, U at most 1 for EDF) and the exact response-time analysis, priority inversion and priority inheritance, and short definitions of gang, fair-share, guaranteed and lottery scheduling.

Why it exists

FCFS, SJF, priority and round robin each fit one kind of system and fail in another. Real systems mix kinds of work: interactive jobs that want a quick response, batch jobs that want throughput, system processes that must not be delayed. A single queue and a single rule cannot serve all of them. Multilevel queues keep one queue per kind of work; the feedback queue lets a process move between queues according to how it behaves, which approximates shortest-job-first without knowing the bursts in advance. And in real-time systems the question is not "how fast on average?" but "does every job finish before its deadline?", which needs different algorithms (rate monotonic, earliest deadline first) and a test of whether a task set can be scheduled at all.

Prerequisites

You need CPU scheduling (FCFS, SJF, SRTF, turnaround, waiting and response time) and priority and round-robin scheduling (quantum, preemption, starvation and aging). The part on priority inversion uses locks, see critical sections and semaphores.

Precise definitions

Multilevel queue. The ready queue is split into separate queues by the kind of process (for example system, interactive, batch). A process stays in its queue for ever. Each queue has its own algorithm (round robin for interactive, FCFS for batch). The queues are scheduled against each other either by fixed priority (a lower queue runs only when all higher queues are empty; starvation of the low queues is possible) or by time slicing between queues (for example 80 per cent of the time to the interactive queue, 20 per cent to batch).

Multilevel feedback queue. Like the multilevel queue, but a process moves between queues. The scheduler is defined by: the number of queues, the algorithm and quantum of each queue, the rule to move a process down (it used its whole quantum: it is CPU-bound), the rule to move it up (it waited too long: aging; or it gave up the CPU before the quantum ended), and the queue that a new process enters. The version used on this page: three queues with quanta 2, 4 and FCFS; a new process enters the top queue; using the whole quantum moves it one queue down; a ready process in a higher queue pre-empts a running process of a lower queue.

Periodic real-time task. A task τi\tau_i with period TiT_i and worst-case execution time CiC_i releases a job every TiT_i time units; each job must finish within its relative deadline DiD_i after its release (usually Di=TiD_i = T_i). Utilisation of a set: U=∑iCi/TiU = \sum_i C_i / T_i. Hard real time: a missed deadline is a failure; soft real time: it only lowers the quality.

Rate-monotonic (RM) scheduling. Static priorities: the shorter the period, the higher the priority; preemptive. Earliest-deadline-first (EDF). Dynamic priorities: at every moment the ready job with the nearest absolute deadline runs; preemptive. (Absolute deadline of a job = its release time plus DiD_i.)

Priority inversion. A high-priority job waits for a lock held by a low-priority job, and a medium-priority job keeps the low one from running, so the high-priority job is delayed by a job of lower priority. Priority inheritance: the lock holder runs with the priority of the highest job it blocks, until it releases the lock.

How it works

The feedback queue

Multilevel feedback queue: start

Schedule simulated, not typed

Multilevel feedback queue (quanta 2, 4, then FCFS). Context-switch time is ignored.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
running
P1
ready queue (front first)
empty
processarrivalburstleftstatusfinishturnaroundwaitingresponse
P1099running
P2133not arrived
P3222not arrived
P4655not arrived
average turnaround
-
average waiting
-
average response
-

Step 0

Four processes arrive at times 0, 1, 2 and 6 with bursts 9, 3, 2 and 5. Three queues: level 0 has quantum 2, level 1 quantum 4, level 2 is first come, first served. A new process enters level 0. A process that uses its whole quantum moves one level down; a higher level always runs before a lower one, and an arrival at a higher level pre-empts the running process (it goes to the tail of its own level with a fresh quantum).

0 / 11
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

Four processes with bursts 9, 3, 2 and 5. Everything enters the top queue with a quantum of 2. P1P_1 runs 2 units and has not finished, so it sinks to the middle queue; the same for P2P_2; P3P_3 (burst 2) finishes inside its first quantum and never leaves the top. When P4P_4 arrives at time 6 it enters the top queue and runs first, ahead of the older but lower P1P_1. The long process P1P_1 ends in the last queue, which is FCFS, and runs last. The short jobs have been favoured without anyone knowing their bursts: a job that is short finishes while it is still in a high queue.

The feedback queue approximates shortest-job-first: start

Computed by a script, not typed
policyAverage turnaroundAverage waitingAverage response
First come, first served
Round robin, quantum 4
Shortest remaining time first (needs the bursts in advance)
Multilevel feedback queue 2, 4, FCFS

Step 0

One long job P1 (burst 20, arrives at 0) and four short jobs arriving at 1, 3, 5 and 8 with bursts 2, 3, 2 and 4. The feedback queue does not know the bursts but favours the jobs that are short by letting a job that uses up its quantum sink.

0 / 4
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

On a mix of one long and four short jobs the feedback queue has an average response time close to that of SRTF (which needs to know the bursts), a lower turnaround than round robin and a far lower one than FCFS.

Real-time: rate monotonic and earliest deadline first

Two tasks: T1T_1 (period 5, needs 2) and T2T_2 (period 7, needs 4). Utilisation 2/5+4/7=0.9712/5 + 4/7 = 0.971.

Rate-monotonic scheduling: start

Schedule simulated, not typed

Rate monotonic (fixed priority: the shorter the period, the higher the priority), preemptive. Context-switch time is ignored.

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
running
T1.1
ready queue (front first)
T2.1
processarrivalburstpriorityleftstatusfinishturnaroundwaitingresponse
T1.10252running
T1.25252not arrived
T1.310252not arrived
T1.415252not arrived
T1.520252not arrived
T1.625252not arrived
T1.730252not arrived
T2.10474ready
T2.27474not arrived
T2.314474not arrived
T2.421474not arrived
T2.528474not arrived
average turnaround
-
average waiting
-
average response
-

Step 0

Two periodic tasks released together at time 0: T1 has period 5 and needs 2 units each period, T2 has period 7 and needs 4. Each job (T1.1, T1.2, ...) must finish before the next release of its task. Utilisation = 2/5 + 4/7 = 0.971. Rate monotonic gives T1 the higher priority because its period is shorter. In the table 'Priority' is the period.

0 / 28
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

Rate monotonic gives T1T_1 the higher priority. T2.1T_2.1 (released at 0, deadline 7) gets the CPU only between the jobs of T1T_1 and has not finished at time 7: deadline missed. The CPU is almost fully used and still a job is late, because the fixed priorities cannot move the work around. The same tasks with EDF:

Earliest deadline first: start

Schedule simulated, not typed

Earliest deadline first (dynamic priority: the job with the nearest absolute deadline runs), preemptive. Context-switch time is ignored.

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
running
T1.1
ready queue (front first)
T2.1
processarrivalburstpriorityleftstatusfinishturnaroundwaitingresponse
T1.10252running
T1.252102not arrived
T1.3102152not arrived
T1.4152202not arrived
T1.5202252not arrived
T1.6252302not arrived
T1.7302352not arrived
T2.10474ready
T2.274144not arrived
T2.3144214not arrived
T2.4214284not arrived
T2.5284354not arrived
average turnaround
-
average waiting
-
average response
-

Step 0

The same two tasks. Earliest deadline first runs, at every moment, the ready job whose absolute deadline is nearest (ties: T1 first). In the table 'Priority' is the absolute deadline of the job.

0 / 28
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

At time 5, the new job T1.2T_1.2 (deadline 10) has a later deadline than T2.1T_2.1 (deadline 7), so T2.1T_2.1 keeps the CPU and finishes in time. No job misses its deadline in the whole hyperperiod (35 time units).

Is the task set schedulable?

For deadline equal to period and one processor, two classical results (Liu and Layland):

  • EDF meets all deadlines if and only if U≤1U \le 1.
  • RM meets all deadlines if U≤n(21/n−1)U \le n(2^{1/n} - 1) (a sufficient condition). The bound is 1 for one task, 0.828 for two, 0.780 for three, and decreases to ln⁡2≈0.693\ln 2 \approx 0.693 for many tasks. Above the bound the test says nothing: the set may or may not be schedulable.

RM is the best fixed-priority assignment for such task sets (if any fixed-priority order works, RM works), and EDF is the best of all.

Schedulability tests against simulation: start

Computed by a script, not typed
task setURM boundU ≤ 1U ≤ RM boundResponse-time test passesRate monotonic (simulated)EDF (simulated)
two tasks, low load: (4, 1), (6, 1)
three tasks, above the bound: (4, 1), (6, 2), (12, 3)
harmonic periods, U = 1: (2, 1), (4, 1), (8, 2)
U below 1, rate monotonic fails: (5, 2), (7, 4)
U below 1, rate monotonic fails (three tasks): (4, 1), (5, 2), (6, 2)
U above 1: (3, 2), (4, 2)

Step 0

Each row is a set of periodic tasks (period, execution time), deadline = period, all released at time 0. U = sum of C/T. Rate monotonic is guaranteed to work if U ≤ n(2^(1/n) - 1) (sufficient, not necessary); earliest deadline first works exactly when U ≤ 1. The last columns are simulations over one hyperperiod.

0 / 6
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

Each row compares the tests with a simulation over a whole hyperperiod. Row 2: U=0.833U = 0.833 is above the RM bound 0.780, yet RM does schedule the set; the bound is only sufficient. Row 3: periods 2, 4, 8 are harmonic (each divides the next): RM works up to U=1U = 1. Rows 4 and 5: UU is below 1, so EDF works, but RM fails. Row 6: U>1U > 1, nothing can work.

The exact test for fixed priorities: response-time analysis

With the tasks in priority order and all released together, the worst case for task ii is its first job (the critical instant). Its response time is Ri=Ci+∑j with higher priority⌈Ri/Tj⌉ CjR_i = C_i + \sum_{j \text{ with higher priority}} \lceil R_i / T_j \rceil\, C_j: its own work plus all the jobs of higher-priority tasks released before it finishes. Start with R=CiR = C_i and iterate until RR stops changing; the task meets its deadline exactly when the final R≤DiR \le D_i.

Response-time analysis: start

Computed by a script, not typed
taskFormulaIterationResponse timeMeets its deadline
T1 (period 4, C = 1)
T2 (period 6, C = 2)
T3 (period 12, C = 3)

Step 0

Priorities by period: T1 highest. A task is delayed by every higher-priority job released before it finishes, so its response time R satisfies R = C + sum of ceil(R/T_j) x C_j (j with higher priority). Iterate from R = C.

0 / 3
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

For the three-task set whose utilisation 0.833 is above the bound, the iteration for T3T_3 goes 3, 6, 7, 9, 10, 10: the worst-case response time is 10, within the period 12. So the first job of T3T_3 completes at time 10 (with RM and all tasks released at 0).

Priority inversion

Priority inversion and priority inheritance: start

Computed by a script, not typed
job012345678910111213
H, no inheritance
M, no inheritance
L, no inheritance
H, with inheritance
M, with inheritance
L, with inheritance

Step 0

Three jobs share one lock. L (low priority) arrives at 0 and needs 6 units, three of them inside the critical section (units 2 to 4, it takes the lock after one unit of work). M (medium) arrives at 2 and needs 5 units. H (high) arrives at 3 and needs 3 units, its first unit is inside the critical section. The cells show what each job does in every time unit (RUN, ready, blocked).

0 / 14
computed by script: tick-by-tick simulation of the scheduling policies of scripts/os_rt.py. python3 scripts/gen-os-rt-traces.py

LL takes the lock and is pre-empted by MM. HH arrives and needs the lock: it is blocked. Without inheritance MM keeps running because it has a higher priority than LL; LL cannot release the lock until MM is finished. HH, the most important job, waits for the less important MM (five units) as well as for LL: priority inversion. With inheritance, as soon as HH blocks, LL runs with HH's priority, finishes its critical section, releases the lock, and HH goes on; the delay of HH is now bounded by the length of the critical section. (A well-known real case is the Mars Pathfinder lander in 1997, where a priority inversion on a lock caused repeated resets until priority inheritance was switched on; this is from the literature, not computed here.)

Other policies you may meet (quoted from the textbooks, not computed)

PolicyIdea
Gang schedulingthe threads of one parallel application are scheduled together, at the same time on different processors, so that threads that wait for each other do not wait for a thread that is not running
Fair-share schedulingthe CPU is divided among users (or groups), not among processes: a user with many processes does not get more than the others
Guaranteed schedulingpromise each of nn users 1/n1/n of the CPU; always run the process whose ratio (CPU time received) / (CPU time entitled) is lowest
Lottery schedulingevery process holds lottery tickets; the scheduler draws a ticket at random, so the expected share is proportional to the number of tickets
Policy versus mechanismthe kernel provides the mechanism (for example priorities), the user process chooses the policy (for example sets the priorities of its threads)

Worked examples

EX 1Rate-monotonic priorities and the utilisation test

Easy

Tasks (period, execution time): A=(10,2)A = (10, 2), B=(5,1)B = (5, 1), C=(20,3)C = (20, 3). Rate monotonic: the shortest period has the highest priority, so BB first, then AA, then CC. U=2/10+1/5+3/20=0.2+0.2+0.15=0.55U = 2/10 + 1/5 + 3/20 = 0.2 + 0.2 + 0.15 = 0.55. The bound for three tasks is 3(21/3−1)=0.7803(2^{1/3} - 1) = 0.780. Since 0.55≤0.7800.55 \le 0.780, RM is guaranteed to meet all deadlines. Since 0.55≤10.55 \le 1, EDF also works.

EX 2Where RM fails and EDF does not

Medium

Tasks T1=(5,2)T_1 = (5, 2) and T2=(7,4)T_2 = (7, 4), both released at 0. U=0.971U = 0.971, above the two-task RM bound 0.828 and below 1. RM: T1T_1 runs in [0,2)[0,2), T2T_2 in [2,5)[2,5) (3 of its 4 units); at 5 the new T1T_1 job pre-empts and runs [5,7)[5,7); T2T_2 gets its last unit in [7,8)[7,8) and finishes at 8, after its deadline 7: it misses. EDF: at time 5 the deadlines are 10 (T1.2T_1.2) and 7 (T2.1T_2.1), so T2T_2 continues and finishes at 6; no deadline is missed.

EX 3When does the first job of the lowest-priority task finish?

Medium

Tasks (4,1)(4, 1), (6,2)(6, 2), (12,3)(12, 3), all released at time 0, RM. Simulate: T1T_1 runs [0,1)[0,1), T2T_2 runs [1,3)[1,3), T3T_3 runs [3,4)[3,4) (1 of 3 units); at 4, T1.2T_1.2 runs [4,5)[4,5); T3T_3 continues [5,6)[5,6) (2 units done); at 6, T2.2T_2.2 runs [6,8)[6,8); at 8, T1.3T_1.3 runs [8,9)[8,9); T3T_3 runs [9,10)[9,10) and has its 3 units: it finishes at 10, which is also the response-time iteration's answer. A first job of a task finishes at the response time computed by the iteration (all tasks released together).

EX 4Typical questions

GATE level

(a) "Which scheduling algorithm is optimal for periodic tasks with deadlines equal to periods on one processor?" EDF. "Among static-priority algorithms?" Rate monotonic. (b) "A set of tasks has U=0.9U = 0.9 with 3 tasks. Is RM guaranteed to work? Is EDF?" RM: the test fails (0.9>0.7800.9 > 0.780), so not guaranteed (may or may not work); EDF: yes, U≤1U \le 1. (c) "Match: gang scheduling, rate-monotonic scheduling, fair-share scheduling with: guaranteed scheduling, real-time scheduling, thread scheduling." Gang goes with thread scheduling (groups of threads together), rate monotonic with real-time, fair-share with guaranteed share. (d) "In a multilevel feedback queue, a process that uses its whole quantum is...?" Moved down: it behaves CPU-bound. (e) "Can the lowest queue of a multilevel queue with fixed priorities starve?" Yes. (f) "What is priority inversion and how is it cured?" A high-priority job waits for a lower one; by priority inheritance (or priority ceiling). (g) "Which statement is true: U≤1U \le 1 is necessary for any algorithm on one processor." True, since the CPU cannot do more than one unit of work per unit of time.

Cheat box

Cheat box

  • Multilevel queue: fixed classes, no movement. Feedback queue: a process that uses its whole quantum moves down; short and interactive jobs stay high; approximates SJF without knowing bursts. Fixed priority between queues can starve the low ones.
  • Utilisation U=∑Ci/TiU = \sum C_i / T_i. Necessary for any algorithm: U≤1U \le 1.
  • RM: shorter period = higher priority, static. Sufficient test U≤n(21/n−1)U \le n(2^{1/n} - 1) (1, 0.828, 0.780, ... to ln⁡2=0.693\ln 2 = 0.693); not necessary. Harmonic periods: works up to U=1U = 1.
  • EDF: nearest absolute deadline first, dynamic. Works iff U≤1U \le 1 (deadline = period).
  • Exact RM test: Ri=Ci+∑j<i⌈Ri/Tj⌉CjR_i = C_i + \sum_{j<i} \lceil R_i / T_j \rceil C_j, iterate from CiC_i; need Ri≤DiR_i \le D_i.
  • Priority inversion: high waits on a lock of low while medium runs. Fix: priority inheritance.
  • Gang = threads of a job together; fair share = per user; guaranteed = promised 1/n1/n; lottery = random tickets.

Traps and GATE patterns

Connections

The feedback queue is a refinement of priority and round-robin scheduling: the priority changes with behaviour, the quantum depends on the level. Real-time scheduling is CPU scheduling with a different aim, deadlines instead of averages. Priority inversion is a meeting of scheduling with locks and semaphores. Gang scheduling concerns threads that run in parallel.

PYQs

2 previous year questions from your book. I re-solved each one myself (1 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2015 · NATQ33 · page 300 of the book
Consider a uniprocessor system executing three tasks
T1, T2 and T3, each of which is composed of an
infinite sequence of jobs (or instances) which arrive
periodically at intervals of 3, 7 and 20 milliseconds,
respectively. The priority of each task is the inverse
of its period and the available tasks are scheduled in
order of priority, with the highest priority task
scheduled first. Each instance of T1, T2 and T3
requires an execution time of 1, 2 and 4 milliseconds,
respectively. Given that all tasks initially arrive at the
beginning of the 1st millisecond and task preemptions
are allowed, the first instance of T3 completes its
execution
at
the
end
of
______________
milliseconds.
GATE 2007 · MCQ · 1 markQ49 · page 304 of the book
Group 1 contains some CPU scheduling algorithms
and Group 2 contains some applications. Match
entries in Group 1 to entries in Group 2.

Group 1
P. Gang Scheduling
Q. Rate Monotonic Scheduling
R. Fair Share Scheduling

Group 2
1.
Guaranteed Scheduling
2.
Real-time Scheduling
3.
Thread Scheduling
  • (a)P-3; Q-2; R-1
  • (b)P-1; Q-2; R-3
  • (c)P-2; Q-3; R-1
  • (d)P-1; Q-3; R-2

Self-check

Question 1

Three tasks have (C,T)(C, T) equal to (1,3)(1, 3), (1,4)(1, 4) and (2,6)(2, 6). Compute UU. Is RM guaranteed by the bound? Is EDF schedulable?

Show answer

U=1/3+1/4+2/6=0.333+0.25+0.333=0.917U = 1/3 + 1/4 + 2/6 = 0.333 + 0.25 + 0.333 = 0.917. The bound for three tasks is 0.780, so RM is not guaranteed by the test (it may still work). EDF: U≤1U \le 1, so yes.

Question 2

Why does a feedback queue favour short jobs although it does not know their lengths?

Show answer

A new job starts in the top queue with a small quantum. A short job finishes before it has used up quanta and so never moves down; a long job uses its whole quantum again and again and sinks to lower queues, where it runs only when the higher queues are empty.

Question 3

HH needs a lock held by LL. MM (priority between them) is ready. Who runs without inheritance and with inheritance?

Show answer

Without: MM (the highest-priority job that is not blocked), LL stays pre-empted and HH waits. With: LL runs, with the priority of HH, until it releases the lock; then HH runs.

Revision card

One-screen revision

  • Multilevel queue: fixed classes. Feedback queue: used the whole quantum means moved down; approximates SJF.
  • U=∑C/TU = \sum C/T; U≤1U \le 1 is necessary. RM: sufficient U≤n(21/n−1)U \le n(2^{1/n} - 1), to 0.693; exact: R=C+∑⌈R/Tj⌉CjR = C + \sum \lceil R/T_j \rceil C_j. EDF: iff U≤1U \le 1.
  • RM static (short period first), EDF dynamic (nearest deadline).
  • Priority inversion cured by inheritance. Gang, fair share, guaranteed, lottery: see the table.

References

  • ·Silberschatz, Galvin, Gagne, Operating System Concepts (10th edition), Chapter 5 CPU Scheduling (multilevel queue, multilevel feedback queue, real-time CPU scheduling: rate-monotonic, earliest-deadline-first)
  • ·Tanenbaum, Bos, Modern Operating Systems (4th edition), Section 2.4 Scheduling (scheduling in batch, interactive and real-time systems, policy versus mechanism, thread scheduling)
  • ·Liu, Layland, Scheduling Algorithms for Multiprogramming in a Hard-Real-Time Environment, Journal of the ACM 20(1), 1973, the utilisation bound for rate-monotonic scheduling and the optimality of earliest deadline first

Verification

scripts/enum-checks/os_rt_check.py: (1) a second real-time simulator (priority queue of released jobs) gives exactly the same timeline as scripts/os_rt.py for rate monotonic and earliest deadline first on 2806 random task sets (1 to 4 tasks, periods 2 to 12, hyperperiod at most 400); (2) the exact response-time analysis equals the simulation: the task meets all deadlines exactly when the iteration stays within the period, and the iteration's value is the largest response time seen in the simulation; (3) with deadline equal to period, earliest deadline first meets every deadline exactly when U is at most 1; U at most n(2^(1/n) - 1) always implied that rate monotonic works; sets above the bound that rate monotonic still schedules (216) and sets with U at most 1 where it fails (18) exist, and for those 18 no other fixed priority order works either (all orders tried); harmonic periods with U at most 1 always worked; (4) the multilevel feedback queue of scripts/os_rt.py equals a second implementation that works in slices instead of ticks on 1500 random process sets with six lists of quanta; bursts, arrivals and idle time are consistent; (5) the priority inversion timelines equal the ones derived by hand. tests/os-rt-traces.test.ts re-simulates the real-time tables in TypeScript. CONVENTIONS: all tasks are released together at time 0 (the critical instant); deadline equals period; a job that misses its deadline keeps running; ties go to the task listed first; a process pre-empted in the feedback queue goes to the tail of its own level with a fresh quantum; a process that uses its whole quantum moves one level down; no input or output and no aging in the model; priority inheritance is the basic protocol with one lock. Books differ on these conventions, a question states its own. NOT COMPUTED, quoted from the textbooks: gang, fair-share, guaranteed and lottery scheduling, hard and soft real time, the Mars Pathfinder episode. Chapter references are from memory and need verification. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never