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 with period and worst-case execution time releases a job every time units; each job must finish within its relative deadline after its release (usually ). Utilisation of a set: . 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 .)
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 typedMultilevel feedback queue (quanta 2, 4, then FCFS). Context-switch time is ignored.
| process | arrival | burst | left | status | finish | turnaround | waiting | response |
|---|---|---|---|---|---|---|---|---|
| P1 | 0 | 9 | 9 | running | ||||
| P2 | 1 | 3 | 3 | not arrived | ||||
| P3 | 2 | 2 | 2 | not arrived | ||||
| P4 | 6 | 5 | 5 | not arrived |
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).
Four processes with bursts 9, 3, 2 and 5. Everything enters the top queue with a quantum of 2. runs 2 units and has not finished, so it sinks to the middle queue; the same for ; (burst 2) finishes inside its first quantum and never leaves the top. When arrives at time 6 it enters the top queue and runs first, ahead of the older but lower . The long process 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| policy | Average turnaround | Average waiting | Average 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.
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: (period 5, needs 2) and (period 7, needs 4). Utilisation .
Rate-monotonic scheduling: start
Schedule simulated, not typedRate monotonic (fixed priority: the shorter the period, the higher the priority), preemptive. Context-switch time is ignored.
| process | arrival | burst | priority | left | status | finish | turnaround | waiting | response |
|---|---|---|---|---|---|---|---|---|---|
| T1.1 | 0 | 2 | 5 | 2 | running | ||||
| T1.2 | 5 | 2 | 5 | 2 | not arrived | ||||
| T1.3 | 10 | 2 | 5 | 2 | not arrived | ||||
| T1.4 | 15 | 2 | 5 | 2 | not arrived | ||||
| T1.5 | 20 | 2 | 5 | 2 | not arrived | ||||
| T1.6 | 25 | 2 | 5 | 2 | not arrived | ||||
| T1.7 | 30 | 2 | 5 | 2 | not arrived | ||||
| T2.1 | 0 | 4 | 7 | 4 | ready | ||||
| T2.2 | 7 | 4 | 7 | 4 | not arrived | ||||
| T2.3 | 14 | 4 | 7 | 4 | not arrived | ||||
| T2.4 | 21 | 4 | 7 | 4 | not arrived | ||||
| T2.5 | 28 | 4 | 7 | 4 | not arrived |
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.
Rate monotonic gives the higher priority. (released at 0, deadline 7) gets the CPU only between the jobs of 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 typedEarliest deadline first (dynamic priority: the job with the nearest absolute deadline runs), preemptive. Context-switch time is ignored.
| process | arrival | burst | priority | left | status | finish | turnaround | waiting | response |
|---|---|---|---|---|---|---|---|---|---|
| T1.1 | 0 | 2 | 5 | 2 | running | ||||
| T1.2 | 5 | 2 | 10 | 2 | not arrived | ||||
| T1.3 | 10 | 2 | 15 | 2 | not arrived | ||||
| T1.4 | 15 | 2 | 20 | 2 | not arrived | ||||
| T1.5 | 20 | 2 | 25 | 2 | not arrived | ||||
| T1.6 | 25 | 2 | 30 | 2 | not arrived | ||||
| T1.7 | 30 | 2 | 35 | 2 | not arrived | ||||
| T2.1 | 0 | 4 | 7 | 4 | ready | ||||
| T2.2 | 7 | 4 | 14 | 4 | not arrived | ||||
| T2.3 | 14 | 4 | 21 | 4 | not arrived | ||||
| T2.4 | 21 | 4 | 28 | 4 | not arrived | ||||
| T2.5 | 28 | 4 | 35 | 4 | not arrived |
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.
At time 5, the new job (deadline 10) has a later deadline than (deadline 7), so 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 .
- RM meets all deadlines if (a sufficient condition). The bound is 1 for one task, 0.828 for two, 0.780 for three, and decreases to 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 set | U | RM bound | U ≤ 1 | U ≤ RM bound | Response-time test passes | Rate 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.
Each row compares the tests with a simulation over a whole hyperperiod. Row 2: 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 . Rows 4 and 5: is below 1, so EDF works, but RM fails. Row 6: , 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 is its first job (the critical instant). Its response time is : its own work plus all the jobs of higher-priority tasks released before it finishes. Start with and iterate until stops changing; the task meets its deadline exactly when the final .
Response-time analysis: start
Computed by a script, not typed| task | Formula | Iteration | Response time | Meets 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.
For the three-task set whose utilisation 0.833 is above the bound, the iteration for goes 3, 6, 7, 9, 10, 10: the worst-case response time is 10, within the period 12. So the first job of 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| job | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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).
takes the lock and is pre-empted by . arrives and needs the lock: it is blocked. Without inheritance keeps running because it has a higher priority than ; cannot release the lock until is finished. , the most important job, waits for the less important (five units) as well as for : priority inversion. With inheritance, as soon as blocks, runs with 's priority, finishes its critical section, releases the lock, and goes on; the delay of 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)
| Policy | Idea |
|---|---|
| Gang scheduling | the 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 scheduling | the CPU is divided among users (or groups), not among processes: a user with many processes does not get more than the others |
| Guaranteed scheduling | promise each of users of the CPU; always run the process whose ratio (CPU time received) / (CPU time entitled) is lowest |
| Lottery scheduling | every process holds lottery tickets; the scheduler draws a ticket at random, so the expected share is proportional to the number of tickets |
| Policy versus mechanism | the 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
EasyTasks (period, execution time): , , . Rate monotonic: the shortest period has the highest priority, so first, then , then . . The bound for three tasks is . Since , RM is guaranteed to meet all deadlines. Since , EDF also works.
EX 2Where RM fails and EDF does not
MediumTasks and , both released at 0. , above the two-task RM bound 0.828 and below 1. RM: runs in , in (3 of its 4 units); at 5 the new job pre-empts and runs ; gets its last unit in and finishes at 8, after its deadline 7: it misses. EDF: at time 5 the deadlines are 10 () and 7 (), so continues and finishes at 6; no deadline is missed.
EX 3When does the first job of the lowest-priority task finish?
MediumTasks , , , all released at time 0, RM. Simulate: runs , runs , runs (1 of 3 units); at 4, runs ; continues (2 units done); at 6, runs ; at 8, runs ; runs 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 with 3 tasks. Is RM guaranteed to work? Is EDF?" RM: the test fails (), so not guaranteed (may or may not work); EDF: yes, . (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: 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 . Necessary for any algorithm: .
- RM: shorter period = higher priority, static. Sufficient test (1, 0.828, 0.780, ... to ); not necessary. Harmonic periods: works up to .
- EDF: nearest absolute deadline first, dynamic. Works iff (deadline = period).
- Exact RM test: , iterate from ; need .
- 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 ; lottery = random tickets.
Traps and GATE patterns
Connections
Across subjects
No cross-subject links yet.
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
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.
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 equal to , and . Compute . Is RM guaranteed by the bound? Is EDF schedulable?
Show answerHide
. The bound for three tasks is 0.780, so RM is not guaranteed by the test (it may still work). EDF: , so yes.
Question 2
Why does a feedback queue favour short jobs although it does not know their lengths?
Show answerHide
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
needs a lock held by . (priority between them) is ready. Who runs without inheritance and with inheritance?
Show answerHide
Without: (the highest-priority job that is not blocked), stays pre-empted and waits. With: runs, with the priority of , until it releases the lock; then runs.
Revision card
One-screen revision
- Multilevel queue: fixed classes. Feedback queue: used the whole quantum means moved down; approximates SJF.
- ; is necessary. RM: sufficient , to 0.693; exact: . EDF: iff .
- 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
My notes (saved in this browser)