Chapter 5: CPU Scheduling

Operating Systems
CPU–I/O burst cycleDispatcher and context switchScheduling criteria FCFS and the convoy effectSJF, SRTF, exponential averagePriority and aging Round robin and the time quantumMLQ and MLFQLottery, stride, fair share Multiprocessor schedulingReal time: RM and EDFLinux EEVDF, Windows
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Concepts

Explain the CPU–I/O burst cycle, scheduling decision points, preemptive and nonpreemptive scheduling, and the dispatcher's job.

Criteria

Compute waiting, turnaround and response times from a Gantt chart; weigh throughput, CPU utilization and fairness goals.

Algorithms

Apply FCFS, SJF, SRTF, priority, RR, MLQ and MLFQ step by step to the same set of processes and compare them.

Prediction and fairness

Predict the next CPU burst with an exponential average; prevent starvation with aging, lottery, stride and fair-share scheduling.

Multiprocessor and real time

Explain load balancing and processor affinity; test schedulability with RM and EDF using utilization bounds.

Real systems

Describe Linux CFS and its successor EEVDF, Windows priority classes and dynamic priority boosts.

Basic Concepts

What Is Scheduling?

  • In a multiprogrammed system many processes wait ready in memory; when the CPU becomes free, the short-term scheduler (CPU scheduler) decides which one runs.
  • Goal: never leave the CPU idle, and improve the chosen objectives (response, throughput, fairness, deadlines).
  • Ready queue: processes ready to run. Wait queues: those waiting for I/O or an event. Running: at most one per core.
  • The ready queue need not be FIFO: it can be a priority queue, a tree or an unordered list.
  • Modern kernels actually schedule kernel threads; “process scheduling” is used in this sense.
newReady queueP3P7P1P4CPUexittime expired (interrupt)I/O requestI/O wait queueI/O done

Basic Concepts

The CPU–I/O Burst Cycle

load, store, addCPU burstread from file → waitI/O burststore, incrementCPU burstwrite to file → waitI/O burstload, add, storeCPU burstread from network → waitI/O burst⋮
08162432CPU burst duration (ms)frequencymany short burstsfew long bursts
(a) CPU-bound(b) I/O-boundtime →filled: CPU use · gap: I/O wait

What matters is not how long the I/O wait is but how short the CPU burst is. An I/O-bound process that runs as soon as it is ready keeps the disk and network busy.

Basic Concepts

Overlapping with I/O: Treating Bursts as Separate Jobs

A and B each need 50 ms of CPU; A runs 10 ms and then waits 10 ms for the disk, B does no I/O.

No overlap: A's 50 ms of work counts as one job, and B starts only when A endsCPUdiskAAAAABI/OI/OI/OI/O020406080100120140A ends 90, B ends 140 → CPU busy 100 of 140 ms: 71%

If A counts as one 50 ms job, the CPU is idle while A is at the disk: B starts only when A ends.

Overlap: each 10 ms burst of A is a separate short job; B runs while A is at the diskCPUdiskAAAAABBBBBI/OI/OI/OI/O020406080100120140A ends 90, B ends 100 → CPU busy for all 100 ms: 100%

Each burst is a separate job: B runs while A is at the disk; the short returning A preempts B (STCF).

ScheduleA doneB doneAvg. turnaroundCPU utilization
No overlap90140(90 + 140) / 2 = 115 ms100 / 140 ≈ 71%
Overlap90100(90 + 100) / 2 = 95 ms100 / 100 = 100%

A real system that does not know burst lengths learns this behavior with MLFQ.

Basic Concepts

When Are Scheduling Decisions Made?

newreadyrunningterminatedwaitingadmitteddispatchedinterrupt / slice expiredwait for I/O or eventI/O completedexit()

1. Running → waiting: the process requests I/O or calls wait(). The CPU is free; a choice is required.

newreadyrunningterminatedwaitingadmitteddispatchedinterrupt / slice expiredwait for I/O or eventI/O completedexit()

2. Running → ready: a clock interrupt arrived, the time slice expired. Preempting the running process is optional.

newreadyrunningterminatedwaitingadmitteddispatchedinterrupt / slice expiredwait for I/O or eventI/O completedexit()

3. Waiting → ready: I/O completed; the woken process may preempt the running one if it is more important.

newreadyrunningterminatedwaitingadmitteddispatchedinterrupt / slice expiredwait for I/O or eventI/O completedexit()

4. Termination: the process ends with exit(); the CPU is free, a choice is required.

newreadyrunningterminatedwaitingadmitteddispatchedinterrupt / slice expiredwait for I/O or eventI/O completedexit()■ happens even without preemption (1, 4)■ only a preemptive scheduler decides here (2, 3)

A scheduler that decides only at 1 and 4 is nonpreemptive (cooperative); one that also decides at 2 and 3 is preemptive.

Basic Concepts

Preemptive and Nonpreemptive Scheduling

Nonpreemptive

The chosen process runs until it blocks, exits or yields the CPU (yield). No clock interrupt needed; few switches.

Windows 3.x and classic Mac OS (≤ 9) used cooperative multitasking; one faulty program could hang the system.

Preemptive

A process is suspended when its time slice expires or a higher-priority process becomes ready. Needs a clock interrupt; every modern general-purpose OS does this.

The price: race conditions on shared data and more context switches.

NonpreemptiveA (until it requests I/O)BB waited for A to yieldPreemptiveABABA▼ clock interrupt: slice (q = 4) expired
  • Kernel preemption: since Linux 2.6 kernel code is preemptible too, but only when it holds no lock (preempt_count = 0).
  • After 20 years, the PREEMPT_RT patches entered mainline in Linux 6.12 (2024): interrupt handlers run as threads; latency drops to microseconds.
  • Batch jobs can use nonpreemptive or long-slice algorithms; interactive and server systems need preemption.

Basic Concepts

The Dispatcher and the Context Switch

P0kernelP1runningsave to PCB0selectload from PCB1runninginterrupt / system callreturn to user mode, jump to PCdispatch latency
  • The dispatcher gives the CPU to the process the scheduler chose: it switches context, switches to user mode and jumps to the right place in the program.
  • It runs on every switch, so it must be fast; the direct cost today is a few microseconds.
  • Measuring on Linux: the cs column of vmstat 1; voluntary_ctxt_switches and nonvoluntary_ctxt_switches in /proc/<pid>/status.
ComponentOn a process switch
Registers, PC, SPsaved / loaded
Memory mappage-table base changes (x86: CR3)
TLBnot flushed if PCID/ASID exist
Cachesnot flushed, but left cold

Basic Concepts

Scheduling Goals by Environment

EnvironmentMain goalsSuitable approach
All systemsFairness (similar processes get similar shares), policy enforcement (e.g. security processes first), balance (CPU and I/O devices busy together)—
BatchThroughput (jobs/hour), short turnaround time, high CPU utilizationNonpreemptive or long-slice preemptive
InteractiveShort response time, proportionality (delays that match user expectations)Preemptive; short time slice
Real-timeMeeting deadlines (no data loss), predictability (no drop in audio/video quality)Priority / deadline based; tasks run briefly and block
Batch jobs are still common today: payroll and overnight banking runs, scientific computing and AI training (HPC queue managers such as Slurm), CI builds. Goals often conflict: frequent switches that shorten response time lower throughput.

Basic Concepts

Scheduling Criteria

arrival Aready, waitingrunningpreempted, waitingrunningfirst run Fcompletion Cresponse = F − Aturnaround = C − A = CPU time + waiting+ waiting
CriterionDefinitionGoal
CPU utilizationFraction of time the CPU is busy (40–90% in real systems)maximize
ThroughputNumber of processes completed per unit timemaximize
Turnaround timeTime from submission to completion: C − Aminimize
Waiting timeTotal time spent in the ready queue (without I/O: turnaround − CPU time)minimize
Response timeTime from submission until first getting the CPU (the first response): F − Aminimize

The scheduler affects only the time spent waiting in the ready queue; CPU and I/O time belong to the program. In interactive systems reducing the variance of response time matters more than its average. Weighted turnaround = turnaround / CPU time.

Basic Algorithms

A Common Set of Processes

ProcessArrivalCPU timePriority
P1082
P2143
P3291
P4354
  • Smaller number = higher priority; ties go to the earlier arrival.
  • In RR a process that arrives at the same moment enters the queue before the one whose slice just ended.
  • Times in ms; context-switch time taken as 0; no I/O.
P18 msP24 msP39 msP45 ms0510152025CPU time needed from arrival
FCFS, SJF, SRTF, priority and RR are run step by step on these four processes, so the results can be compared directly.

Basic Algorithms

FCFS Step by Step

First-come, first-served: the ready queue is FIFO; a process that gets the CPU keeps it until it finishes or blocks. The simplest algorithm to implement.

P1P2P3P4P10826t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 is in the queue; it takes the CPU and keeps it for 8 ms (nonpreemptive).

P1P2P3P4P1P2081226t = 8CPU: P2 · left 4Ready:P3 · 9P4 · 5

t = 8: P1 is done. The queue advances in arrival order → P2 at the head runs (it waited 7 ms).

P1P2P3P4P1P2P308122126t = 12CPU: P3 · left 9Ready:P4 · 5

t = 12: P2 is done. The queue advances in arrival order → P3 at the head runs (it waited 10 ms).

P1P2P3P4P1P2P3P408122126t = 21CPU: P4 · left 5Ready:(empty)

t = 21: P3 is done. The queue advances in arrival order → P4 at the head runs (it waited 18 ms).

P1P2P3P4P1P2P3P408122126
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P10808800
P2148121177
P3291221191010
P4352126231818
Avg.15.258.758.75

Average waiting 8.75, turnaround 15.25, response 8.75 ms · 3 context switches.

Basic Algorithms

FCFS: The Convoy Effect

Order: P1 → P2 → P3 (all ready at t = 0)P1P2P30242730Waiting: P1 = 0, P2 = 24, P3 = 27 → average (0 + 24 + 27) / 3 = 17 ms

Long job first: the short P2 and P3 wait behind P1. Average waiting 17 ms.

Order: P2 → P3 → P1P2P3P103630Waiting: P1 = 6, P2 = 0, P3 = 3 → average (6 + 0 + 3) / 3 = 3 ms

The same jobs in a different arrival order: average 3 ms. Under FCFS the result is very sensitive to arrival order.

CPUCCdiskCI1I2I3C has a long burst; I1–I3 wait readydisk idleCPU idledisk idle againC: CPU-bound · I1, I2, I3: I/O-bound (1 ms CPU, 3 ms disk)

Convoy effect: the CPU-bound C holds the CPU; the I/O-bound processes pile up behind it and the devices sit idle in turn.

The CPU-bound process holds the CPU until it terminates; I/O-bound processes release it after short bursts. FCFS is nonpreemptive; there is no starvation, but it is unsuitable for interactive systems.

Basic Algorithms · Step through the code

Computing FCFS Waiting Time

/* p[] sorted by arrival time */
void fcfs(Proc p[], int n) {
  int t = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    if (t < p[i].arrival) t = p[i].arrival;
    int wait = t - p[i].arrival;
    t += p[i].burst;         /* completion */
    sum += wait;
    printf("%s: %d\n", p[i].name, wait);
  }
  printf("avg = %.2f\n", (double) sum / n);
}
026

The clock t and the total waiting sum start at zero. The processes are in arrival order.

026

P1 arrived at 0; the CPU was free at 0 → wait = 0 − 0 = 0.

P10826

P1 runs 8 ms; the clock becomes 8; total waiting 0.

P10826

P2 arrived at 1; the CPU was free at 8 → wait = 8 − 1 = 7.

P1P2081226

P2 runs 4 ms; the clock becomes 12; total waiting 7.

P1P2081226

P3 arrived at 2; the CPU was free at 12 → wait = 12 − 2 = 10.

P1P2P308122126

P3 runs 9 ms; the clock becomes 21; total waiting 17.

P1P2P308122126

P4 arrived at 3; the CPU was free at 21 → wait = 21 − 3 = 18.

P1P2P3P408122126

P4 runs 5 ms; the clock becomes 26; total waiting 35.

P1P2P3P408122126

Average waiting = 35 / 4 = 8.75 ms, the same as the FCFS Gantt chart.

Basic Algorithms

SJF: Shortest Job First

  • Shortest-job-first: among the ready processes, pick the one with the shortest next CPU burst; ties are broken FCFS.
  • If all jobs are ready at once, it provably minimizes the average waiting time: the first job's length is added to everyone's wait, so the shortest should go first (in the formula on the right, a has the largest coefficient).
  • If arrivals are at different times, nonpreemptive SJF is not optimal; its preemptive version, SRTF, is.
  • The problem: the next burst length is unknown. Batch systems use the user's estimate (e.g. Slurm --time); short-term scheduling predicts it from history.
  • Long jobs can starve if short jobs keep arriving.
(a) arrival order: turnaround 8, 12, 16, 20 → avg. 14ABCD08121620(b) shortest first: turnaround 4, 8, 12, 20 → avg. 11BCDA0481220avg. turnaround = (4a + 3b + 2c + d) / 4

When all jobs are ready at once, SJF minimizes average waiting and turnaround time; it does not aim to raise CPU utilization, since the CPU is busy under every algorithm as long as there is work.

Basic Algorithms

SJF: All Ready at Once

The classic example: four processes ready at t = 0; CPU times P1 = 6, P2 = 8, P3 = 7, P4 = 3 ms. Under FCFS the same set would have average waiting (0 + 6 + 14 + 21) / 4 = 10.25 ms.

all ready at t = 0P40324t = 0CPU: P4 · left 3Ready:P1 · 6P2 · 8P3 · 7

t = 0: all are ready; P4, with the shortest CPU time (3), is chosen.

all ready at t = 0P4P103924t = 3CPU: P1 · left 6Ready:P2 · 8P3 · 7

t = 3: P4 is done. Ready: P1 (6), P2 (8), P3 (7) → shortest P1 (6).

all ready at t = 0P4P1P30391624t = 9CPU: P3 · left 7Ready:P2 · 8

t = 9: P1 is done. Ready: P3 (7), P2 (8) → shortest P3 (7).

all ready at t = 0P4P1P3P20391624t = 16CPU: P2 · left 8Ready:(empty)

t = 16: P3 is done. Ready: P2 (8) → shortest P2 (8).

all ready at t = 0P4P1P3P20391624
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P10639933
P2081624241616
P3079161699
P40303300
Avg.1377

Average waiting 7, turnaround 13, response 7 ms · 3 context switches.

Basic Algorithms

SJF on the Common Set

Nonpreemptive SJF on the common set: at t = 0 only P1 exists and cannot be preempted; every later choice takes the shortest ready process.

P1P2P3P4P10826t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 is ready. SJF is nonpreemptive: short jobs arriving later cannot stop P1.

P1P2P3P4P1P2081226t = 8CPU: P2 · left 4Ready:P3 · 9P4 · 5

t = 8: P1 is done. Ready: P2 (4), P3 (9), P4 (5) → shortest P2 (4).

P1P2P3P4P1P2P408121726t = 12CPU: P4 · left 5Ready:P3 · 9

t = 12: P2 is done. Ready: P3 (9), P4 (5) → shortest P4 (5).

P1P2P3P4P1P2P4P308121726t = 17CPU: P3 · left 9Ready:(empty)

t = 17: P4 is done. Ready: P3 (9) → shortest P3 (9).

P1P2P3P4P1P2P4P308121726
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P10808800
P2148121177
P3291726241515
P43512171499
Avg.14.257.757.75

Average waiting 7.75, turnaround 14.25, response 7.75 ms · 3 context switches.

Basic Algorithms

Comparing FCFS and SJF

ABCDEAvg.
Arrival01234
CPU43524
SJFCompletion4918613
Turnaround4816398
Weighted12.673.21.52.252.12
FCFSCompletion47121418
Turnaround461011149
Weighted1225.53.52.8
Weighted (normalized) turnaround = turnaround / CPU time; 1 is ideal. FCFS penalizes short jobs: in the set A(0, 1), B(1, 100), C(2, 1), D(3, 100), the 1 ms job C waits 99 ms behind B and its weighted turnaround is 100 (1.99 for D).

Weighted turnaround for C is 16 / 5 = 3.2; the SJF average is 2.12.

Basic Algorithms

Predicting the Next CPU Burst: Exponential Average

τn+1 = α · tn + (1 − α) · τn,  0 ≤ α ≤ 1

  • tn: actual length of the n-th burst; τn: the prediction made for it; τ0: a constant (e.g. the system average).
  • α = 0 → the last measurement is ignored and the prediction never changes; α = 1 → only the last burst counts. A common choice is α = ½.
  • Expanded: τn+1 = αtn + (1−α)αtn−1 + … + (1−α)n+1τ0: older measurements weigh exponentially less.
  • Tanenbaum's “aging” estimate is the same idea: T0, T0/2 + T1/2, T0/4 + T1/4 + T2/2, …
  • Example (α = ½, τ0 = 10): t0 = 6 → τ1 = ½·6 + ½·10 = 8.
24681012t646413131310866591112τactual burstprediction τ

Basic Algorithms · Step through the code

Computing the Exponential Average

double tau = 10.0;      /* τ0 */
const double a = 0.5;  /* α */
int t[] = { 6, 4, 6, 4, 13, 13, 13 };
for (int n = 0; n < 7; n++) {
  printf("%.1f %d\n", tau, t[n]);
  tau = a * t[n] + (1 - a) * tau;
}
printf("next: %.1f\n", tau);

Initial prediction τ0 = 10, α = 0.5. No burst has been measured yet.

6

n = 0: actual 6, predicted 10 → τ = 0.5·6 + 0.5·10 = 8.

64

n = 1: actual 4, predicted 8 → τ = 0.5·4 + 0.5·8 = 6.

646

n = 2: actual 6, predicted 6 → τ = 0.5·6 + 0.5·6 = 6.

6464

n = 3: actual 4, predicted 6 → τ = 0.5·4 + 0.5·6 = 5.

646413

n = 4: actual 13, predicted 5 → τ = 0.5·13 + 0.5·5 = 9.

64641313

n = 5: actual 13, predicted 9 → τ = 0.5·13 + 0.5·9 = 11.

6464131313

n = 6: actual 13, predicted 11 → τ = 0.5·13 + 0.5·11 = 12.

6464131313

The bursts jumped to 13; the prediction catches up in a few steps: 5 → 9 → 11 → 12.

Basic Algorithms

SRTF Step by Step

Shortest-remaining-time-first (SRTF / SRTN): the preemptive version of SJF. When a new process arrives, its length is compared with the remaining time of the running one.

P1P2P3P4P10126t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 exists and starts running.

P1P2P3P4P1P201226t = 1CPU: P2 · left 4Ready:P1 · 7

t = 1: P2 arrives (4); the running P1 has 7 left. 4 < 7 → P1 is preempted and P2 runs.

P1P2P3P4P1P2P2012326t = 2CPU: P2 · left 3Ready:P1 · 7P3 · 9

t = 2: P3 arrives (9). The running P2 has 3 left ≤ 9 → no preemption.

P1P2P3P4P1P2P2013526t = 3CPU: P2 · left 2Ready:P1 · 7P3 · 9P4 · 5

t = 3: P4 arrives (5). The running P2 has 2 left ≤ 5 → no preemption.

P1P2P3P4P1P2P40151026t = 5CPU: P4 · left 5Ready:P1 · 7P3 · 9

t = 5: P2 is done. Remaining times: P1 7, P3 9, P4 5 → shortest P4 (5).

P1P2P3P4P1P2P4P1015101726t = 10CPU: P1 · left 7Ready:P3 · 9

t = 10: P4 is done. Remaining times: P1 7, P3 9 → shortest P1 (7).

P1P2P3P4P1P2P4P1P3015101726t = 17CPU: P3 · left 9Ready:(empty)

t = 17: P1 is done. Remaining times: P3 9 → shortest P3 (9).

P1P2P3P4P1P2P4P1P3015101726
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P1080171790
P21415400
P3291726241515
P435510722
Avg.136.54.25

Average waiting 6.5, turnaround 13, response 4.25 ms · 4 context switches.

P1 starts at t = 0, is preempted at 1 and resumes where it left off at 10. The average waiting time is 6.5 ms.

Basic Algorithms

Priority Scheduling

  • Each process gets a priority number; the highest-priority ready process runs. In this course a smaller number = higher priority (Linux and Windows differ in direction).
  • Priorities come from internal factors (memory, open files, measured I/O/CPU ratio) or external ones (importance, payment, policy). SJF is the special case where the priority is the predicted next burst; if all priorities are equal, the algorithm becomes FCFS.
  • It can be preemptive or nonpreemptive; RR can be used among processes of the same priority (b).
  • A side effect is priority inversion: while a high-priority job waits for a lock held by a low-priority one, medium-priority jobs cut in; the fix is priority inheritance.
(a) Nonpreemptive priority · P1 (10, pr 3), P2 (1, pr 1), P3 (2, pr 4), P4 (1, pr 5), P5 (5, pr 2), all at t = 0P2P5P1P3P4016161819Waiting: P1 6, P2 0, P3 16, P4 18, P5 1 → average 41 / 5 = 8.2 ms(b) Priority + RR (q = 2) · P1 (4, pr 3), P2 (5, pr 2), P3 (8, pr 2), P4 (7, pr 1), P5 (3, pr 3)P4P2P3P2P3P2P3P1P5P1P5079111315162022242627Waiting: P1 22, P2 11, P3 12, P4 0, P5 24 → average 13.8 ms

Basic Algorithms

Preemptive Priority on the Common Set

Preemptive priority on the common set (P1 pr 2, P2 pr 3, P3 pr 1, P4 pr 4). A newly arrived process preempts the running one if it has higher priority.

P1P2P3P4P10126t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 exists and starts running.

P1P2P3P4P1P101226t = 1CPU: P1 · left 7Ready:P2 · pr 3 · 4

t = 1: P2 arrives (priority 3). The running P1 has higher priority (2) → no preemption.

P1P2P3P4P1P302326t = 2CPU: P3 · left 9Ready:P1 · pr 2 · 6P2 · pr 3 · 4

t = 2: P3 arrives (priority 1); the running P1 has lower priority (2) → it is preempted and P3 runs.

P1P2P3P4P1P3P30231126t = 3CPU: P3 · left 8Ready:P1 · pr 2 · 6P2 · pr 3 · 4P4 · pr 4 · 5

t = 3: P4 arrives (priority 4). The running P3 has higher priority (1) → no preemption.

P1P2P3P4P1P3P102111726t = 11CPU: P1 · left 6Ready:P2 · pr 3 · 4P4 · pr 4 · 5

t = 11: P3 is done. Ready: P1 (pr 2), P2 (pr 3), P4 (pr 4) → highest priority P1.

P1P2P3P4P1P3P1P20211172126t = 17CPU: P2 · left 4Ready:P4 · pr 4 · 5

t = 17: P1 is done. Ready: P2 (pr 3), P4 (pr 4) → highest priority P2.

P1P2P3P4P1P3P1P2P40211172126t = 21CPU: P4 · left 5Ready:(empty)

t = 21: P2 is done. Ready: P4 (pr 4) → highest priority P4.

P1P2P3P4P1P3P1P2P40211172126
ProcessArrivalCPUPriorityFirst runCompletionTurnaroundWaitingResponse
P10820171790
P21431721201616
P3291211900
P43542126231818
Avg.17.2510.758.5

Average waiting 10.75, turnaround 17.25, response 8.5 ms · 4 context switches.

Basic Algorithms

Starvation and Aging

Starvation: if high-priority jobs keep arriving, a low-priority job never runs. Aging: its priority is raised step by step while it waits. Rule: for every 4 ms spent ready, the priority number drops by 1. L (3 ms, pr 7) arrives at t = 0; a new H (4 ms, pr 2) arrives every 4 ms. Nonpreemptive priority.

H1 + LH10428t = 0L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 0: L's priority is 7, the newly arrived H1's is 2 → H1 runs. After L waits 4 ms its priority becomes 6.

H1 + LH2H1H204828t = 4L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 4: L's priority is 6, the newly arrived H2's is 2 → H2 runs. After L waits another 4 ms its priority becomes 5.

H1 + LH2H3H1H2H30481228t = 8L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 8: L's priority is 5, the newly arrived H3's is 2 → H3 runs. After L waits another 4 ms its priority becomes 4.

H1 + LH2H3H4H1H2H3H4048121628t = 12L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 12: L's priority is 4, the newly arrived H4's is 2 → H4 runs. After L waits another 4 ms its priority becomes 3.

H1 + LH2H3H4H5H1H2H3H4H504812162028t = 16L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 16: L's priority is 3, the newly arrived H5's is 2 → H5 runs. After L waits another 4 ms its priority becomes 2.

H1 + LH2H3H4H5H6H1H2H3H4H5L0481216202328t = 20L's priority:7t=06t=45t=84t=123t=162t=20H priority: 2

t = 20: L's priority is 2, equal to H6's; ties go to the earlier arrival → L finally runs (it waited 20 ms).

H1H2H3H4H5LH6048121620232728Without aging: as long as H keeps coming, L's priority stays at 7 and L never runs (starvation).

Aging turns an unbounded wait into a bounded one: in the worst case L waits priority gap × 4 ms = 20 ms.

Round Robin

Round Robin (RR)

  • Round robin: FCFS + preemption. Each process runs for at most one time quantum (q), then goes to the tail of the queue.
  • With n ready processes (a fixed number) and quantum q, ignoring context switches, no process waits longer than (n − 1) · q: response time is bounded, and there is no starvation.
  • Turnaround is usually worse than SJF, response time better.
  • An I/O-bound process uses a small part of its quantum and blocks for I/O (moving to a wait queue), returning to the tail of the ready queue when the I/O completes; CPU-bound processes get a bigger share (hence refinements such as MLFQ and virtual RR).
(a) runnable processesBFDGAcurrent: B(b) after B uses its quantumFDGABB to the tail, F runs
Example: q = 20 · P1 53, P2 17, P3 68, P4 24 (all at t = 0)thin line at 154: two consecutive quanta of P3P1P2P3P4P1P3P4P1P3P302037577797117121134154162Waiting: P1 81, P2 20, P3 94, P4 97 → avg. 73 ms · avg. response 28.5 ms (SJF: avg. waiting 38)

Round Robin

RR Step by Step: q = 2

The common set, q = 2 ms. A small quantum: processes swap often; every process gets its first turn quickly.

P1P2P3P4P10226t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 exists; it runs for at most q = 2 ms, then gives up its turn.

P1P2P3P4P1P202426t = 2CPU: P2 · left 4Ready:P3 · 9P1 · 6

t = 2: P1 used up its quantum (6 left) and goes to the tail. P2 and P3, which arrived meanwhile, entered the queue before it. Next: P2.

P1P2P3P4P1P2P3024626t = 4CPU: P3 · left 9Ready:P1 · 6P4 · 5P2 · 2

t = 4: P2 used up its quantum (2 left) and goes to the tail. P4, which arrived meanwhile, entered the queue before it. Next: P3.

P1P2P3P4P1P2P3P10246826t = 6CPU: P1 · left 6Ready:P4 · 5P2 · 2P3 · 7

t = 6: P3 used up its quantum (7 left) and goes to the tail. Next: P1.

P1P2P3P4P1P2P3P1P4024681026t = 8CPU: P4 · left 5Ready:P2 · 2P3 · 7P1 · 4

t = 8: P1 used up its quantum (4 left) and goes to the tail. Next: P4.

P1P2P3P4P1P2P3P1P4P202468101226t = 10CPU: P2 · left 2Ready:P3 · 7P1 · 4P4 · 3

t = 10: P4 used up its quantum (3 left) and goes to the tail. Next: P2.

P1P2P3P4P1P2P3P1P4P2P30246810121426t = 12CPU: P3 · left 7Ready:P1 · 4P4 · 3

t = 12: P2 finished before its quantum ran out. Next: P3.

P1P2P3P4P1P2P3P1P4P2P3P1024681012141626t = 14CPU: P1 · left 4Ready:P4 · 3P3 · 5

t = 14: P3 used up its quantum (5 left) and goes to the tail. Next: P1.

P1P2P3P4P1P2P3P1P4P2P3P1P402468101214161826t = 16CPU: P4 · left 3Ready:P3 · 5P1 · 2

t = 16: P1 used up its quantum (2 left) and goes to the tail. Next: P4.

P1P2P3P4P1P2P3P1P4P2P3P1P4P30246810121416182026t = 18CPU: P3 · left 5Ready:P1 · 2P4 · 1

t = 18: P4 used up its quantum (1 left) and goes to the tail. Next: P3.

P1P2P3P4P1P2P3P1P4P2P3P1P4P3P1024681012141618202226t = 20CPU: P1 · left 2Ready:P4 · 1P3 · 3

t = 20: P3 used up its quantum (3 left) and goes to the tail. Next: P1.

P1P2P3P4P1P2P3P1P4P2P3P1P4P3P1P402468101214161820222326t = 22CPU: P4 · left 1Ready:P3 · 3

t = 22: P1 finished before its quantum ran out. Next: P4.

P1P2P3P4P1P2P3P1P4P2P3P1P4P3P1P4P30246810121416182022232526t = 23CPU: P3 · left 3Ready:(empty)

t = 23: P4 finished before its quantum ran out. Next: P3.

P1P2P3P4P1P2P3P1P4P2P3P1P4P3P1P4P3P30246810121416182022232526t = 25CPU: P3 · left 1Ready:(empty)

t = 25: P3 used up its quantum (1 left), but no other process is in the queue → it gets a new quantum with no context switch.

P1P2P3P4P1P2P3P1P4P2P3P1P4P3P1P4P302468101214161820222326
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P10802222140
P2142121171
P32942624152
P43582320155
Avg.19.2512.752

Average waiting 12.75, turnaround 19.25, response 2 ms · 12 context switches.

Round Robin

RR Step by Step: q = 5

The common set, q = 5 ms. A larger quantum: fewer switches, but longer response time.

P1P2P3P4P10526t = 0CPU: P1 · left 8Ready:(empty)

t = 0: only P1 exists; it runs for at most q = 5 ms, then gives up its turn.

P1P2P3P4P1P205926t = 5CPU: P2 · left 4Ready:P3 · 9P4 · 5P1 · 3

t = 5: P1 used up its quantum (3 left) and goes to the tail. P2, P3 and P4, which arrived meanwhile, entered the queue before it. Next: P2.

P1P2P3P4P1P2P30591426t = 9CPU: P3 · left 9Ready:P4 · 5P1 · 3

t = 9: P2 finished before its quantum ran out. Next: P3.

P1P2P3P4P1P2P3P4059141926t = 14CPU: P4 · left 5Ready:P1 · 3P3 · 4

t = 14: P3 used up its quantum (4 left) and goes to the tail. Next: P4.

P1P2P3P4P1P2P3P4P105914192226t = 19CPU: P1 · left 3Ready:P3 · 4

t = 19: P4 finished before its quantum ran out. Next: P1.

P1P2P3P4P1P2P3P4P1P305914192226t = 22CPU: P3 · left 4Ready:(empty)

t = 22: P1 finished before its quantum ran out. Next: P3.

P1P2P3P4P1P2P3P4P1P305914192226
ProcessArrivalCPUFirst runCompletionTurnaroundWaitingResponse
P10802222140
P21459844
P32992624157
P4351419161111
Avg.17.5115.5

Average waiting 11, turnaround 17.5, response 5.5 ms · 5 context switches.

Round Robin · Step through the code

Round Robin: Code Trace

/* p[] sorted by arrival time */
void rr(Proc p[], int n, int q) {
  int t = 0, k = 0;     /* k: next arrival */
  add_arrivals(p, n, &k, t);
  while (!empty() || k < n) {
    if (empty()) {       /* CPU idle */
      t = p[k].arrival;  /* next arrival */
      add_arrivals(p, n, &k, t);
    }
    Proc *s = dequeue(); /* head */
    int slice = min(q, s->left);
    t += slice; s->left -= slice;
    add_arrivals(p, n, &k, t);  /* first */
    if (s->left > 0) enqueue(s); /* then */
    else printf("%s %d\n", s->name, t);
  }
}
026CPU: idleReady:P1·8

t = 0: only P1 has arrived; it enters the queue. The quantum is q = 4 ms.

P10426P1: left 4Ready:P2·4P3·9P4·5P1·4

t = 0–4: P1 leaves the queue and runs 4 ms (4 left). Meanwhile P2, P3, P4 arrived; they enter the queue first, then P1.

P1P204826P2: left 0Ready:P3·9P4·5P1·4

t = 8: P2 ran its last 4 ms and is done; its completion time is printed.

P1P2P30481226P3: left 5Ready:P4·5P1·4P3·5

t = 12: P3 used up its quantum (5 left) and returns to the tail.

P1P2P3P4048121626P4: left 1Ready:P1·4P3·5P4·1

t = 16: P4 used up its quantum (1 left) and returns to the tail.

P1P2P3P4P104812162026P1: left 0Ready:P3·5P4·1

t = 20: P1 ran its last 4 ms and is done; its completion time is printed.

P1P2P3P4P1P30481216202426P3: left 1Ready:P4·1P3·1

t = 24: P3 used up its quantum (1 left) and returns to the tail.

P1P2P3P4P1P30481216202426P4: left 0Ready:P3·1

t = 25: P4 finished its last 1 ms before the quantum ran out (slice = 1); it does not go back in the queue.

P1P2P3P4P1P30481216202426P3: left 0Ready:(empty)

t = 26: P3 finished its last 1 ms before the quantum ran out (slice = 1); it does not go back in the queue.

P1P2P3P4P1P30481216202426Waiting: P1 12 · P2 3 · P3 15 · P4 17average waiting 11.75 · turnaround 18.25 · response 4.5 ms

The queue is empty and no process is left to arrive (k = n), so the loop ends: 8 dispatches, 7 context switches. New arrivals are queued before the preempted process because of the order of lines 13 and 14. Lines 6–9 never run in this example.

Round Robin

Choosing the Time Quantum: A Trade-off

QuantumWaitingTurnaroundResponseSwitches
q = 212.7519.25212
q = 313.52039
q = 411.7518.254.57
q = 51117.55.55
q = 61218.56.55
q ≥ 9 (FCFS)8.7515.258.753

Averages in ms; switches = number of context switches.

avg. response (ms)22334.545.556.568.759context switches1229374555639time quantum q (ms)
  • As q shrinks, response improves (2 ms at q = 2) but context switches increase (12 switches); on a real system each switch costs CPU time.
  • Average waiting and turnaround do not change monotonically with q (q = 3 is worse than q = 2). Once q ≥ 9 (the longest burst), RR is exactly FCFS.

Round Robin

Time Quantum and Context Switches

q = 120 switchesq = 61 switchq = 19 switches0610a single 10 ms process
91011121311111.5210.75311.5412.25510.5610.57time quantum q · P1 6, P2 3, P3 1, P4 7avg. turnaround
  • Fraction lost = s / (q + s) (each process uses its full quantum, with a switch after each one; it differs for processes that finish early or block): with q = 4 ms and switch time s = 1 ms, 20% of the CPU is wasted; at q = 100 ms it is ≈ 1%, but the system responds sluggishly.
  • Today a switch takes a few µs and a typical quantum is 1–100 ms (Linux EEVDF base slice 0.75 ms × (1 + log₂ CPUs), 3 ms on 8+ cores; on Windows client ≈ 2 clock ticks). Rule of thumb: ≈ 80% of CPU bursts should be shorter than q.
  • Turnaround does not improve monotonically with q (right). With q = 4, the set P1 24, P2 3, P3 3 has average waiting 17 / 3 ≈ 5.67 ms (17 under FCFS).

Multilevel Queues

Multilevel Queue (MLQ)

  • The ready queue is split into classes; each process is permanently assigned to one queue by type (e.g. foreground / background).
  • Each queue has its own algorithm: RR (short q) for the foreground, FCFS for the background.
  • Scheduling among queues: fixed priority (a lower queue runs only when the upper one is empty → starvation) or time slicing (e.g. 80% of the CPU to the foreground, 20% to the background).
  • Low overhead but inflexible: a process cannot change queues.
highest priorityreal-time processessystem processesinteractive processesbatch processeslowest priorityCPU

A structure with fixed assignment is MLQ; one where processes change queues based on their behavior is the feedback queue, MLFQ.

Multilevel Queues

Multilevel Feedback Queue (MLFQ)

Q0 · q = 8 msused up quantum → lower queueQ1 · q = 16 msused up quantum → lower queueQ2 · FCFSCPUnew jobQ1 runs if Q0 is empty; Q2 if Q0 and Q1 are empty
  1. Priority(A) > Priority(B) → A runs; if equal, RR.
  2. A new job enters the top queue (assumed short at first).
  3. A job that uses up its allotment at a level moves down one level, no matter how many times it gave up the CPU.
  4. Every S time units all jobs move to the top queue (priority boost): this prevents starvation and gives a job whose behavior changed a new chance.

Parameters: number of queues, each queue's algorithm and quantum, the rules for moving down/up, the entry queue for new jobs. The rules follow OSTEP (Chapter 8); short jobs finish at the top, long CPU jobs sink down.

Multilevel Queues

MLFQ Step by Step

Three queues: Q0 (q = 8), Q1 (q = 16), Q2 (FCFS). Jobs: A (0, 30), B (2, 6), C (4, 20), D (25, 4). A preempted job returns to the head of its queue and completes its quantum.

ABCDA0860t = 0CPU: A · Q0 · q 8Q0 (8)Q1 (16)Q2 (FCFS)

t = 0: A arrives and enters Q0; it runs with an 8 ms quantum. B (t = 2) and C (t = 4) are added to Q0.

ABCDA·Q0B081460t = 8CPU: B · Q0 · q 8Q0 (8)C · 20Q1 (16)A · 22Q2 (FCFS)

t = 8: A used up its quantum → down to Q1. B, next in Q0, runs.

ABCDA·Q0B·Q0C08142260t = 14CPU: C · Q0 · q 8Q0 (8)Q1 (16)A · 22Q2 (FCFS)

t = 14: B finished in 6 ms (before its quantum ran out, in Q0). Now C runs 8 ms in Q0.

ABCDA·Q0B·Q0C·Q0A0814222560t = 22CPU: A · Q1 · q 16Q0 (8)Q1 (16)C · 12Q2 (FCFS)

t = 22: C also moves down to Q1. Q0 is empty → A at the head of Q1 runs with a 16 ms quantum.

ABCDA·Q0B·Q0C·Q0AD081422252960t = 25CPU: D · Q0 · q 8Q0 (8)Q1 (16)A · 19C · 12Q2 (FCFS)

t = 25: D arrives and enters Q0: since it is the upper queue, it preempts A. A returns to the head of Q1 (13 ms of its quantum left).

ABCDA·Q0B·Q0C·Q0ADA08142225294260t = 29CPU: A · Q1 · q left 13Q0 (8)Q1 (16)C · 12Q2 (FCFS)

t = 29: D finished in 4 ms (response 0, waiting 0). A uses the remaining 13 ms of its Q1 quantum.

ABCDA·Q0B·Q0C·Q0ADA·Q1C0814222529425460t = 42CPU: C · Q1 · q 16Q0 (8)Q1 (16)Q2 (FCFS)A · 6

t = 42: A used up its Q1 quantum too → Q2. C runs in Q1; 12 ms is enough.

ABCDA·Q0B·Q0C·Q0ADA·Q1C·Q1A0814222529425460t = 54CPU: A · Q2 · FCFSQ0 (8)Q1 (16)Q2 (FCFS)

t = 54: Q0 and Q1 are empty; A runs its last 6 ms FCFS in Q2 and finishes at t = 60.

ABCDA·Q0B·Q0C·Q0ADA·Q1C·Q1A·Q20814222529425460
JobArrivalCPUCompletionTurnaroundWaitingResponse
A0306060300
B26141266
C42054503010
D25429400
Avg.31.516.54

The short B and D finish quickly in the top queue (response 6 and 0); the long A sinks down. Average response 4 ms.

Multilevel Queues

MLFQ in Practice

Gaming the scheduler

A job that does I/O just before its quantum ends can monopolize the CPU under the “blocked jobs stay on top” rule. The fix: count the total usage at a level (rule 3).

Starvation and changing behavior

Many interactive jobs starve long ones; and a long job that later becomes interactive goes unnoticed. The fix: periodic priority boost (rule 4).

History: CTSS (1962)

Priority classes ran for 1, 2, 4, 8 quanta; a job that used up its quanta moved down a class. Pressing Enter at the terminal moved the process to the top class; users noticed and sped up their jobs by pressing Enter.

Today

The Solaris TS class (table-driven quanta and priorities), FreeBSD ULE, Windows dynamic priorities and macOS use the MLFQ idea. Since 2.6.23 Linux uses fair sharing (CFS, then EEVDF) instead of priority heuristics.

q = 10 ms9.99.99.99.99.9quantum never used up → always Q0With rule 3Q0Q1Q1total 10 ms at this level used up → moves down a level

Proportional Share

Guaranteed and Fair-Share Scheduling

Guaranteed scheduling: with n users (or processes), each is promised 1/n of the CPU. For each, ratio = CPU received / CPU entitled is computed, and the lowest ratio runs. One at 0.5 (half its share) goes ahead; one at 2.0 waits.

Fair-share scheduling: shares go not to processes but to users or groups; a user who starts many processes cannot get more CPU.

Today: group scheduling with Linux cgroup v2 cpu.weight; systemd puts each user in a user-UID.slice, and Kubernetes CPU requests are turned into weights too.

per-process RR: user 1 gets 80%ABCDEABCDE50% per userAEBECEDEAEuser 1 gets a 2/3 shareABECDEABECblue: user 1 (A, B, C, D) · orange: user 2 (E)

Guaranteed scheduling in Tanenbaum's sense is the share guarantee above, not running to a predetermined timetable.

Proportional Share

Lottery Scheduling

A: 100 tickets0B: 50100C: 250 tickets150400draw: 300 → C winsProbabilities: A 100/400 = 25% · B 12.5% · C 62.5%
  • Each process gets tickets; in every time slice a random ticket is drawn and its holder runs. In the long run, share = tickets / total tickets.
  • Simple to implement: a random number + a running total over a list (Waldspurger and Weihl, 1994).
  • A new process has a chance as soon as it gets tickets: responsive; priority means “more tickets”.
  • Ticket transfer: a client waiting on a server hands its tickets to the server. Ticket currency and, in a trusted environment, inflation.
  • It is probabilistic: it can be unfair in the short run (two jobs with equal tickets may split 10 slices 7–3). Its deterministic counterpart is stride scheduling.

Lottery scheduling is one of the simplest algorithms; priority is expressed as a number of tickets.

Proportional Share · Step through the code

Lottery Scheduling: Code Trace

/* one draw per time slice */
int counter = 0;
int winner = getrandom(0, totaltickets);
node_t *current = head;
while (current) {
  counter = counter + current->tickets;
  if (counter > winner)
    break;          /* found the winner */
  current = current->next;
}
/* current: process that gets the CPU */
run(current);
A: 300B: 1030C: 6040100winner = 47

Ticket list A 30, B 10, C 60 (100 in total). The number drawn is 47; counter holds the running total.

A: 300B: 1030C: 6040100winner = 47counter = 30

A: counter = 30, not above 47 → the drawn ticket is not in A's range; next node.

A: 300B: 1030C: 6040100winner = 47counter = 40

B: counter = 40, not above 47 → the drawn ticket is not in B's range; next node.

A: 300B: 1030C: 6040100winner = 47counter = 100 · winner: C

C: counter = 100 > 47 → C wins; the loop breaks.

A: 300B: 1030C: 6040100winner = 35

Next slice: a new draw, 35; the counter starts from zero and the list is scanned from the head again.

A: 300B: 1030C: 6040100winner = 35counter = 30

A: counter = 30, not above 35 → the drawn ticket is not in A's range; next node.

A: 300B: 1030C: 6040100winner = 35counter = 40 · winner: B

B: counter = 40 > 35 → B wins; the loop breaks.

A: 300B: 1030C: 6040100winner = 12

Next slice: a new draw, 12; the counter starts from zero and the list is scanned from the head again.

A: 300B: 1030C: 6040100winner = 12counter = 30 · winner: A

A: counter = 30 > 12 → A wins; the loop breaks.

Draws: 47 → C · 35 → B · 12 → AExpected share: A 30% · B 10% · C 60%In three slices each ran once:short-run share is not proportional to tickets

In three slices A, B and C each ran once; the share approaches 30 : 10 : 60 only in the long run. Sorting the list by tickets in decreasing order shortens the scan.

The code is the decision loop from OSTEP Chapter 9; the drawn numbers are fixed for the example.

Proportional Share

Stride Scheduling Step by Step

Stride = large number / tickets (10,000 / tickets): A 100, B 200, C 40. In each slice the process with the smallest pass value runs and adds its stride to its pass; ties go in the order A, B, C.

A · 100 tickets · stride 100pass = 0B · 50 tickets · stride 200pass = 0C · 250 tickets · stride 40pass = 0order:A

Step 1: pass values A 0, B 0, C 0 → smallest A runs; pass(A) += 100.

A · 100 tickets · stride 100pass = 100B · 50 tickets · stride 200pass = 0C · 250 tickets · stride 40pass = 0order:AB

Step 2: pass values A 100, B 0, C 0 → smallest B runs; pass(B) += 200.

A · 100 tickets · stride 100pass = 100B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 0order:ABC

Step 3: pass values A 100, B 200, C 0 → smallest C runs; pass(C) += 40.

A · 100 tickets · stride 100pass = 100B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 40order:ABCC

Step 4: pass values A 100, B 200, C 40 → smallest C runs; pass(C) += 40.

A · 100 tickets · stride 100pass = 100B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 80order:ABCCC

Step 5: pass values A 100, B 200, C 80 → smallest C runs; pass(C) += 40.

A · 100 tickets · stride 100pass = 100B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 120order:ABCCCA

Step 6: pass values A 100, B 200, C 120 → smallest A runs; pass(A) += 100.

A · 100 tickets · stride 100pass = 200B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 120order:ABCCCAC

Step 7: pass values A 200, B 200, C 120 → smallest C runs; pass(C) += 40.

A · 100 tickets · stride 100pass = 200B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 160order:ABCCCACC

Step 8: pass values A 200, B 200, C 160 → smallest C runs; pass(C) += 40.

A · 100 tickets · stride 100pass = 200B · 50 tickets · stride 200pass = 200C · 250 tickets · stride 40pass = 200order:ABCCCACC

In 8 slices A ran 2 times, B once, C 5 times: exactly 100 : 50 : 250 = 2 : 1 : 5. With a lottery this ratio holds only in the long run.

Stride is the deterministic form of lottery (Waldspurger, 1995). Its drawback is global state: a newcomer with pass 0 would monopolize the CPU, so it starts at the current minimum pass. The vruntime of Linux CFS is a very close idea.

Multiprocessor Scheduling

Thread Scheduling: PCS and SCS

  • The kernel schedules kernel threads, not processes. A library manages user threads.
  • PCS (process-contention scope): in the N:1 and M:N models the library has user threads compete for the LWPs within their process.
  • SCS (system-contention scope): the kernel chooses among all threads in the system. Linux, Windows and macOS, with the 1:1 model, use only SCS.
  • A current PCS example: Java 21 virtual threads and Go goroutines are mapped onto a few carrier threads by the run-time's own scheduler.
user threadsLWPLWPPCS: library picksSCS: kernel picks
pthread_attr_t attr;
int scope;
pthread_attr_init(&attr);
pthread_attr_getscope(&attr, &scope);
if (scope == PTHREAD_SCOPE_PROCESS)
  printf("PCS\n");
/* Linux, macOS: SYSTEM only */
pthread_attr_setscope(&attr,
                      PTHREAD_SCOPE_SYSTEM);
pthread_create(&tid, &attr, runner, NULL);

Multiprocessor Scheduling

Approaches to Multiprocessor Scheduling

(a) common ready queueT0T1T2T3T4lockcore 0core 1core 2core 3(b) per-core queuescore 0core 1core 2core 3
  • Asymmetric (AMP): a single master processor does all scheduling and I/O; simple but a bottleneck.
  • Symmetric (SMP): each processor schedules itself; most modern general-purpose systems. AMP-like arrangements are still used in embedded/heterogeneous systems.
  • (a) Common queue: the load balances itself, but there is lock contention and poor cache affinity.
  • (b) Per-core queues (two-level scheduling): a job is first assigned to a core, then runs from that core's queue. Locking is almost nil and caches stay warm. Linux, Windows and FreeBSD use this.
  • The price: queues can become unbalanced → load balancing is needed.

Multiprocessor Scheduling

Multicore and Hardware Threads

single-threaded corewaits for memorywaits for memorywaits for memorySMT core: thread 0MMMMMthread 1MMMMMMtime →blue: compute (C) · dashed: memory stall (M)
  • Memory stall: on cache misses a core can spend up to 50% of its time waiting for data.
  • SMT (Intel Hyper-Threading, AMD SMT): 2 hardware threads per core; one runs while the other waits. The OS sees each hardware thread as a separate logical CPU.
  • Two-level scheduling: the OS chooses which software thread runs on which logical CPU; the core's hardware chooses which hardware thread executes at the moment.
  • Sibling logical CPUs share execution units: putting two heavy jobs on the same core while a physical core is idle is a bad decision.

Multiprocessor Scheduling

Load Balancing

CPU0CPU1CPU2CPU3idle

Start: CPU0's queue has 5 jobs, CPU3 is idle. With per-core queues the imbalance does not fix itself.

CPU0CPU1CPU2CPU3push: 2 jobs

Push migration: a periodic balancer compares loads and pushes 2 jobs from the overloaded CPU0 to CPU3.

CPU0CPU1CPU2idleCPU3

The only job on CPU2 finished; CPU2 is now idle.

CPU0CPU1CPU2CPU3pull: 1 job

Pull migration: the idle CPU2 steals a job from the busiest queue. Linux uses both.

CPU0CPU1CPU2CPU3

Balance is restored, but the moved job's cache is cold on the new CPU. Linux balances first within SMT siblings and shared caches, and less often across NUMA nodes (scheduling domains).

Multiprocessor Scheduling · Step through the code

Load Balancing: Code Trace

void balance(int me) {    /* idle CPU */
  int max = me;
  for (int c = 0; c < NCPU; c++)
    if (q[c].n > q[max].n) /* busier */
      max = c;
  int t = (q[max].n - q[me].n) / 2;
  for (int k = 0; k < t; k++)
    migrate(&q[max], &q[me]); /* from tail */
}
CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

CPU 1's queue is empty. Pull balancing: balance(1). Initially max = me = 1.

CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

c = 0: n = 3 > the busiest so far → max = 0.

CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

c = 1: n = 0, not busier.

CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

c = 2: n = 5 > the busiest so far → max = 2.

CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

c = 3: n = 2, not busier.

CPU 0ABCn = 3CPU 1 (me)n = 0CPU 2DEFGHn = 5CPU 3IJn = 2

t = (5 − 0) / 2 = 2: moving half the difference brings the two queues close to equal (CPU 2: 3, CPU 1: 2).

CPU 0ABCn = 3CPU 1 (me)Hn = 1CPU 2DEFGn = 4CPU 3IJn = 2

k = 0: H, at the tail of CPU 2's queue, migrates to CPU 1. Jobs at the tail are not the longest-waiting but the most recent arrivals: those with the coldest caches.

CPU 0ABCn = 3CPU 1 (me)HGn = 2CPU 2DEFn = 3CPU 3IJn = 2

k = 1: G, at the tail of CPU 2's queue, migrates to CPU 1. Queue lengths 3, 2, 3, 2. Migrated jobs find a cold cache and TLB on the new CPU: the price of affinity.

Multiprocessor Scheduling

Processor Affinity, NUMA and Heterogeneous Cores

  • Processor affinity: keeping a thread on the CPU where its cache and TLB are warm. Soft: the scheduler tries but does not guarantee it. Hard: an allowed CPU set (sched_setaffinity(), taskset -c 0-3).
  • NUMA: running a thread on the node where its memory is; Linux automatic NUMA balancing moves pages and tasks closer together.
  • Load balancing and affinity conflict: migration restores balance but breaks cache warmth and locality.
NUMA node 0CPU 0CPU 1memory 0NUMA node 1CPU 2CPU 3memory 1interconnectlocal (fast)remote (slow)
Heterogeneous multiprocessing: ARM big.LITTLE / DynamIQ, P/E cores since Intel's 12th generation (Thread Director gives hardware hints), Apple M series. Linux EAS (Energy Aware Scheduling) places work on the core that meets the performance target with the least energy; background jobs go to efficiency cores.

Real-Time Scheduling

Real-Time Systems and Latency

Hard real time

Missing a deadline means system failure: ABS brakes, airbags, industrial robot control, flight control.

Soft real time

Critical tasks get preference but there is no guarantee; a miss lowers quality: video streaming, VoIP, games, audio playback.

eventinterrupt latencyISRconflictsdispatchreal-time processdispatch latencyevent latency
  • Conflicts: preempting any work running in the kernel, and the release of resources held by low-priority processes. Hence a preemptive, priority-based kernel is needed; for hard guarantees, admission control as well.
  • Algorithms can be static (priorities computed in advance, e.g. RM) or dynamic (decisions at run time, e.g. EDF).

Real-Time Scheduling

Periodic Tasks and Rate-Monotonic (RM) Scheduling

tp (period) = d (deadline)period 1period 2period 3
  • Periodic task: 0 ≤ t ≤ d ≤ p; utilization U = t / p.
  • Rate-monotonic (RM): static priority = 1 / p; a shorter period means higher priority. It is optimal among static-priority algorithms.
  • Liu–Layland bound: if ΣU ≤ n(21/n − 1), RM meets every deadline. n = 1: 1 · n = 2: ≈ 0.828 · n = 3: ≈ 0.780 · n → ∞: ln 2 ≈ 0.693. A sufficient condition, not a necessary one.
RM: P1 (p = 50, t = 20) first · P2 (p = 100, t = 35)P1P2P1P1P2P10205070100120150170200

Example: U = 20/50 + 35/100 = 0.40 + 0.35 = 0.75 ≤ 0.828 → schedulable with RM. Recomputed from Silberschatz Figure 5.21.

Real-Time Scheduling

RM and EDF Step by Step

RMEDFP1P202550160▼ deadlines: blue P1 (50, 100, 150) · green P2 (80, 160)

U = 25/50 + 35/80 = 0.5 + 0.4375 = 0.94 > 0.828: no guarantee for RM. RM: P1 (shorter period) first, 0–25; P2 25–50.

RMEDFP1P2P1P2025507580✗ P2 missed its deadline at t = 80 (5 ms short)▼ deadlines: blue P1 (50, 100, 150) · green P2 (80, 160)

t = 50: P1's next job arrives and, with its higher static priority, preempts P2. P2 runs 5 more ms at 75–80 but time is up: deadline missed.

RMEDFP1P2P1P2025507580✗ P2 missed its deadline at t = 80 (5 ms short)P1P202560160▼ deadlines: blue P1 (50, 100, 150) · green P2 (80, 160)

EDF: the deadline sets the priority. At t = 50 P1's deadline is 100 and P2's is 80 → P2 continues and finishes at 60.

RMEDFP1P2P1P2025507580✗ P2 missed its deadline at t = 80 (5 ms short)P1P2P1P2P1P2P10256085100125145150160▼ deadlines: blue P1 (50, 100, 150) · green P2 (80, 160)

At t = 80 P2's next job has deadline 160 and P1's is 100 → P1 runs until 85. At 100, P1 (150) goes first again. Every deadline is met.

RMEDFP1P2P1P2025507580✗ P2 missed its deadline at t = 80 (5 ms short)P1P2P1P2P1P2P10256085100125145150160▼ deadlines: blue P1 (50, 100, 150) · green P2 (80, 160)

For EDF the condition is ΣU ≤ 1 (preemptive, independent tasks, d = p): 0.94 ≤ 1, and there is no miss over the 400 ms hyperperiod. The price: dynamic priorities and unpredictable losses under overload.

Proportional-share scheduling: T shares in total are allocated; an application with N shares gets N/T of the CPU, and admission control rejects requests that exceed the total. Linux SCHED_DEADLINE (3.14+) combines EDF with CBS bandwidth protection.

Real-Time Scheduling · Step through the code

The Liu–Layland Test: Code Trace

int rm_test(Task g[], int n) {
  double U = 0;
  for (int i = 0; i < n; i++)
    U += (double) g[i].t / g[i].p;
  double bound = n * (pow(2, 1.0/n) - 1);
  if (U <= bound) return 1; /* enough */
  if (U <= 1.0) return 0;   /* unknown */
  return -1;            /* impossible */
}
01U = 0

Tasks: T1(p=20, t=5), T2(p=50, t=10), T3(p=100, t=30). The total utilization U starts at zero.

T1 0.2501U = 0.25

T1: 5 / 20 = 0.25 is added → U = 0.25.

T1 0.25T2 0.201U = 0.45

T2: 10 / 50 = 0.2 is added → U = 0.45.

T1 0.25T2 0.2T3 0.301U = 0.75

T3: 30 / 100 = 0.3 is added → U = 0.75.

T1 0.25T2 0.2T3 0.301bound 0.78U = 0.75

bound = 3(21/3 − 1) = 0.7798; U = 0.75 ≤ bound → 1: RM meets every deadline.

01U = 0

The second call, on the set from the RM and EDF example: P1(p=50, t=25), P2(p=80, t=35).

P1 0.501U = 0.5

P1: 25 / 50 = 0.5 is added → U = 0.5.

P1 0.5P2 0.437501U = 0.9375

P2: 35 / 80 = 0.4375 is added → U = 0.9375.

P1 0.5P2 0.437501bound 0.828U = 0.9375

bound = 0.8284; U = 0.9375 exceeds the bound but is ≤ 1 → 0: the test cannot decide. On this set RM really does miss a deadline, while EDF meets them all.

The test is a sufficient condition: if it returns 1, RM certainly works; if it returns 0, a schedule must be drawn to decide (e.g. from 0 to the longest period).

Real-Time Scheduling · Step through the code

The EDF Scheduler: Code Trace

/* A(P=3,C=1) B(P=4,C=1) C(P=6,C=2) */
for (int t = 0; t < 12; t++) {
    for (int i = 0; i < n; i++)
        if (t % P[i] == 0) {      /* new job */
            left[i] = C[i];
            due[i] = t + P[i];
        }
    int s = -1;
    for (int i = 0; i < n; i++)
        if (left[i] > 0 &&
            (s < 0 || due[i] < due[s]))
            s = i;
    if (s >= 0) left[s]--;     /* 1 ms */
}
0123456789101112ABC

Three periodic jobs, U = 1/3 + 1/4 + 2/6 = 0.917 ≤ 1. The common period is 12 ms; a decision is made every millisecond. Arrows mark job arrivals; the due array holds each job's deadline.

A0123456789101112Aleft 0, due 3Bleft 1, due 4Cleft 2, due 6

t = 0: A, B, C start a new period. Ready: A, B, C; deadlines A 3, B 4, C 6 → the earliest, A, runs.

A0B123456789101112Aleft 0, due 3Bleft 0, due 4Cleft 2, due 6

Ready: B, C; deadlines B 4, C 6 → the earliest, B, runs.

A0B1C2A3456789101112Aleft 0, due 6Bleft 0, due 4Cleft 1, due 6

t = 2–3: C → A. Arriving: A at t = 3. Every millisecond the ready job with the earliest deadline is chosen.

A0B1C2A3C456789101112Aleft 0, due 6Bleft 1, due 8Cleft 0, due 6

t = 4: B starts a new period. Ready: B, C; deadlines B 8, C 6 → the earliest, C, runs.

A0B1C2A3C4B56789101112Aleft 0, due 6Bleft 0, due 8Cleft 0, due 6

Ready: B; deadline 8 → the earliest, B, runs.

A0B1C2A3C4B5A6C789101112Aleft 0, due 9Bleft 0, due 8Cleft 1, due 12

t = 6–7: A → C. Arriving: A and C at t = 6. Every millisecond the ready job with the earliest deadline is chosen.

A0B1C2A3C4B5A6C7B89101112Aleft 0, due 9Bleft 0, due 12Cleft 1, due 12

t = 8: B starts a new period. Ready: B, C; deadlines B 12, C 12 → the earliest, B, runs.

A0B1C2A3C4B5A6C7B8A9101112Aleft 0, due 12Bleft 0, due 12Cleft 1, due 12

t = 9: A starts a new period. Ready: A, C; deadlines A 12, C 12 → the earliest, A, runs.

A0B1C2A3C4B5A6C7B8A9C101112Aleft 0, due 12Bleft 0, due 12Cleft 0, due 12

t = 10–11: C → idle. 11 ms of work in 12 ms: 1 ms idle (1 − 0.917). No job missed its deadline.

Real-Time Scheduling

POSIX Real-Time Scheduling

pthread_attr_t attr;
struct sched_param sp;
int policy;
pthread_attr_init(&attr);
pthread_attr_getschedpolicy(&attr, &policy);
/* policy is ignored unless inheritance is off */
pthread_attr_setinheritsched(&attr,
                      PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
sp.sched_priority = 50;          /* 1..99 */
pthread_attr_setschedparam(&attr, &sp);
pthread_create(&tid, &attr, runner, NULL);
PolicyBehavior
SCHED_FIFOFixed priority (1–99), no time slice; runs until it blocks or yields.
SCHED_RRFIFO + time slicing among equal priorities.
SCHED_OTHERThe default (EEVDF on Linux); also BATCH, IDLE.
SCHED_DEADLINELinux: EDF with runtime / deadline / period.
  • A POSIX.1b extension; requires privilege (CAP_SYS_NICE); in the shell, chrt -f 50 ./prog. Linux gives RT jobs at most 0.95 s of every second (sched_rt_runtime_us).

The function names are pthread_attr_getschedpolicy and pthread_attr_setschedpolicy: no underscore between “sched” and “policy”.

Operating-System Examples

Linux: Scheduling Classes

real-time (SCHED_FIFO / RR)normal (nice)099100139nice −20nice +19← high prioritylow priority →
Class (in priority order)PoliciesSelection
stop— (in-kernel)CPU stop, migration; preempts all
deadlineSCHED_DEADLINEEDF + CBS
rtSCHED_FIFO, SCHED_RR100 priority queues + bitmap, O(1)
fairSCHED_NORMAL, SCHED_BATCH, SCHED_IDLECFS (2.6.23–6.5), EEVDF (6.6+)
extSCHED_EXTBPF-written scheduler (sched_ext, 6.12+)
idle—idle loop when there is no work

Kernel priority (lower is higher): 0–99 real-time (99 − rt_priority), 100–139 fair (nice −20 … 19 → 100 … 139). SCHED_RR slice: 100 ms.

Operating-System Examples

Linux CFS: Virtual Runtime

41226812305580leftmost = smallest vruntime → next
  • Completely Fair Scheduler (2.6.23–6.5): instead of a fixed time slice, each task's virtual runtime (vruntime) is tracked; the smallest runs.
  • vruntime += Δ × 1024 / weight. The weight comes from nice: nice 0 → 1024, nice 5 → 335, nice −5 → 3121; each nice step is ≈ ×1.25 (≈ 10% CPU difference).
  • Example: CPU-bound A (nice 0) and B (nice 5): A gets 1024 / 1359 ≈ 75%, B ≈ 25%. When B runs 10 ms, its vruntime grows by 10 × 1024 / 335 ≈ 30.6 ms.
  • A sleeping I/O-bound task's vruntime grows little; when it wakes it goes straight to the front (though tasks that fell behind get only a bounded advantage).
  • The tree: insertion O(log n), the leftmost is cached → selection O(1).

Operating-System Examples · Step through the code

CFS: vruntime Code Trace

#define DELTA 3000  /* 3 ms, in µs */
/* nice 0 → 1024, nice 5 → 335 */
void tick(void) {
  Task *g = leftmost();   /* leftmost */
  run(g, DELTA);
  g->vruntime += DELTA * 1024 / g->weight;
  tree_insert(g);     /* re-sort */
}
A0B0Order:vruntime (µs); short bar = task that fell behind

A (nice 0, weight 1024) and B (nice 5, weight 335) are CPU-bound; both have vruntime 0. On a tie A, inserted first, is leftmost in the tree.

A3,000B0Order:Avruntime (µs); short bar = task that fell behind

A's vruntime (0) is less than or equal to B's (0) → A runs; 3000 × 1024 / 1024 = 3000 is added.

A3,000B9,170Order:ABvruntime (µs); short bar = task that fell behind

Now B is leftmost (0 < 3,000) → B runs; 3000 × 1024 / 335 = 9170 is added: a low weight means fast “aging”.

A6,000B9,170Order:ABAvruntime (µs); short bar = task that fell behind

A's vruntime (3,000) is less than or equal to B's (9,170) → A runs; 3000 × 1024 / 1024 = 3000 is added.

A9,000B9,170Order:ABAAvruntime (µs); short bar = task that fell behind

A is leftmost again (6,000 ≤ 9,170) → A runs, +3000.

A12,000B9,170Order:ABAAAvruntime (µs); short bar = task that fell behind

A is leftmost again (9,000 ≤ 9,170) → A runs, +3000.

A12,000B18,340Order:ABAAABvruntime (µs); short bar = task that fell behind

Now B is leftmost (9,170 < 12,000) → B runs; 3000 × 1024 / 335 = 9170 is added: a low weight means fast “aging”.

A15,000B18,340Order:ABAAABAvruntime (µs); short bar = task that fell behind

A's vruntime (12,000) is less than or equal to B's (18,340) → A runs; 3000 × 1024 / 1024 = 3000 is added.

A18,000B18,340Order:ABAAABAAvruntime (µs); short bar = task that fell behind

A is leftmost again (15,000 ≤ 18,340) → A runs, +3000.

A21,000B18,340Order:ABAAABAAAvruntime (µs); short bar = task that fell behind

A is leftmost again (18,000 ≤ 18,340) → A runs, +3000.

A21,000B18,340Order:ABAAABAAAvruntime (µs); short bar = task that fell behind

In 9 ticks A ran 7 times and B twice; in the long run the share approaches the 1024 : 335 weight ratio (≈ 75% – 25%). There is no fixed time slice; the share comes from the weight.

A simplified tick handler: the real CFS measures time in ns and uses a “scheduler latency” instead of a slice; the selection rule is the same.

Operating-System Examples

EEVDF: The Successor to CFS

EEVDF (Earliest Eligible Virtual Deadline First, Stoica 1995; Linux 6.6+): a task is eligible if it is behind or exactly at its share (lag ≥ 0, i.e. v ≤ V); its virtual deadline is vd = v + slice / weight. Among eligible tasks the one with the earliest vd runs. Three tasks of equal weight:

1011121314151617181920virtual time (ms)A · slice 3vd = 15 · eligibleB · slice 1vd = 13 · eligibleC · slice 3vd = 18 · not eligibleV = 13

v: A 12, B 12, C 15 → V = average = 13. C is ahead (v > V, negative lag): not eligible. A and B are eligible; vd: A 15, B 13 → B (short slice, early deadline).

1011121314151617181920virtual time (ms)A · slice 3vd = 15 · eligibleB · slice 1vd = 14 · eligibleC · slice 3vd = 18 · not eligibleV = 13.33

B ran 1 ms → v = 13, new vd = 14. V = 13.33; eligible: A and B → earliest vd: B.

1011121314151617181920virtual time (ms)A · slice 3vd = 15 · eligibleB · slice 1vd = 15 · not eligibleC · slice 3vd = 18 · not eligibleV = 13.67

B advanced to 14; V = 13.67. Now only A is eligible (B and C are ahead of the average) → A runs its 3 ms slice.

1011121314151617181920virtual time (ms)A · slice 3vd = 18 · not eligibleB · slice 1vd = 15 · eligibleC · slice 3vd = 18 · not eligibleV = 14.67

A ran 3 ms → v = 15, new vd = 18. V = 14.67; only B (v = 14) is eligible → B.

1011121314151617181920virtual time (ms)A · slice 3vd = 18 · eligibleB · slice 1vd = 16 · eligibleC · slice 3vd = 18 · eligibleV = 15

V = 15: all three are eligible. vd: A 18, B 16, C 18 → B. B, asking for a short slice, runs more often but briefly; thanks to the eligibility rule its long-run share is still equal.

Two crisp criteria (eligibility and deadline) replace CFS's latency heuristics; a latency-sensitive task can ask for a short slice with sched_setattr() (6.12+). Weights come from nice, as in CFS.

Operating-System Examples

Windows Scheduling and Priorities

Relative priorityREALTIMEHIGHABOVE_NORMALNORMALBELOW_NORMALIDLE
time-critical311515151515
highest2615121086
above normal251411975
normal241310864
below normal23129753
lowest22118642
idle1611111
  • Preemptive and priority-based: the highest-priority ready thread runs until it blocks, its quantum ends or a higher-priority thread arrives. 32 levels: 1–15 variable, 16–31 real-time, 0 for the zero-page thread. One queue per level; with no work, the idle thread runs.
  • Dynamic boosts in the variable class: on I/O completion (more for keyboard/mouse), for the foreground window and on GUI wakeups the priority rises temporarily, drops by 1 each quantum and never exceeds 15; a thread starved for ≈ 4 seconds is raised to 15 for one quantum. The quantum is ≈ 2 clock ticks on clients, 12 on servers.
8109I/O completed: +2−1 per quantumback to base priority (never above 15)time →

Evaluation and Summary

Evaluating Algorithms

Deterministic modeling

Compute each algorithm's result for a given workload (the Gantt charts in this chapter). Exact and simple, but valid only for that input.

Queueing models

Averages from arrival and service distributions. Little's law: n = λ × W. If λ = 7 processes arrive per second and each stays W = 2 s on average, there are on average n = 14 processes in the system.

Simulation

A programmed model of the system is driven by randomly generated data or a trace recorded from a real system. Accurate, but can take hours.

Implementation

Try the algorithm in a real kernel. Today, with sched_ext, BPF schedulers can be loaded without rebuilding the kernel and A/B-tested in production. The catch: user behavior adapts to the algorithm too.

λ = 7 processes/sCPUexiteach process stays W = 2 s on average → n = λ × W = 7 × 2 = 14

Evaluation and Summary

Comparison on the Common Set

AlgorithmWaitingTurnaroundResponseSwitches
FCFS8.7515.258.753
SJF7.7514.257.753
SRTF6.5134.254
Priority (P)10.7517.258.54
RR q = 212.7519.25212
RR q = 51117.55.55

Averages in ms; P = preemptive; switches = number of context switches.

FCFS8.758.75SJF7.757.75SRTF6.54.25Priority (P)10.758.5RR q = 212.752RR q = 5115.5avg. waitingavg. response (ms)
  • SRTF is best in average waiting (6.5) but needs to know the burst lengths; RR q = 2 is best in response (2) but makes the most switches.
  • Priority optimizes importance, not waiting time. There is no single “best” algorithm: the choice depends on the goal (response, throughput, fairness, deadlines).

Evaluation and Summary

Exercises: Turnaround, Response and Fairness

  1. A, B, C all arrive at t = 0 and each needs 5 ms of CPU. What are the average turnaround and average response time under SJF and RR (q = 1 ms)?
  2. A (arrival 0, length 100), B (10, 10), C (10, 10). What are the average turnaround and response time under nonpreemptive SJF and STCF (SRTF)?
  3. Unfairness metric U = completion time of the first job to finish / that of the second. Two jobs with equal tickets each need L slices. What is U under RR? How does U change with L under lottery?
Answers
  1. SJF (here the same as FCFS): completions 5, 10, 15 → turnaround 10, response (0 + 5 + 10) / 3 = 5 ms. RR: A, B, C finish at 13, 14, 15 → turnaround 14, response (0 + 1 + 2) / 3 = 1 ms. With equal-length jobs RR is best in response and among the worst in turnaround.
  2. SJF: A 0–100, B 100–110, C 110–120 → turnaround (100 + 100 + 110) / 3 ≈ 103.3, response (0 + 90 + 100) / 3 ≈ 63.3 ms. STCF preempts A at t = 10: B 10–20, C 20–30, A 30–120 → turnaround (120 + 10 + 20) / 3 = 50, response (0 + 0 + 10) / 3 ≈ 3.3 ms.
  3. RR (and stride) alternate: one finishes at 2L − 1, the other at 2L → U = (2L − 1) / 2L (0.95 for L = 10). Under lottery the expected U is ≈ 0.82 for L = 10 and ≈ 0.94 for L = 100: it approaches 1 as jobs get longer, while for short jobs chance creates noticeable unfairness.

Evaluation and Summary

Review Questions (1/2)

  1. At which state transitions does a nonpreemptive scheduler make decisions?
  2. P1 = 5, P2 = 2, P3 = 1 ms, all arriving at t = 0 in this order. What is the average waiting time under FCFS and SJF?
  3. α = 0.5 and τ0 = 8. If the measured bursts are 4 and 12 ms, what is the next prediction?
  4. What happens in RR if the time quantum is very large or very small? With q = 4 ms and a 1 ms switch, what fraction is lost?
Answers
  1. Running → waiting (I/O, wait) and termination.
  2. FCFS: (0 + 5 + 7) / 3 = 4; SJF (P3, P2, P1): (0 + 1 + 3) / 3 ≈ 1.33 ms.
  3. τ1 = 0.5·4 + 0.5·8 = 6; τ2 = 0.5·12 + 0.5·6 = 9 ms.
  4. Large: it degenerates to FCFS and response time grows. Small: switching overhead grows. 1 / (4 + 1) = 20%.

Evaluation and Summary

Review Questions (2/2)

  1. Why does SRTF preempt P1 at t = 1 on the common set?
  2. With per-core queues, CPU 1 became idle; CPUs 0, 2 and 3 hold 3, 5 and 2 jobs. How many jobs does pull balancing move, from where, and at what cost?
  3. Are P1 (p = 40, t = 10) and P2 (p = 60, t = 30) schedulable with RM?
  4. How do two CPU-bound tasks with nice 0 and nice 5 share the CPU under CFS? What does “eligible” mean in EEVDF?
Answers
  1. P2 arrives (4 ms) while P1 has 7 ms left: 4 < 7.
  2. From the busiest, CPU 2, (5 − 0) / 2 = 2 jobs move (queues 3, 2, 3, 2). The migrated jobs find a cold cache and TLB on the new CPU.
  3. U = 0.25 + 0.5 = 0.75 ≤ 0.828 → yes (no misses in simulation either).
  4. 1024 : 335 → ≈ 75% / 25%. Eligible: v ≤ V, i.e. it has received no more than its share (lag ≥ 0).

Evaluation and Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 — Chapter 5: CPU Scheduling.
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson — Section 2.4: Scheduling.
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org — Chapters 7–10: Scheduling, MLFQ, Lottery, Multiprocessor.
  • P. Yosifovich, A. Ionescu, M. Russinovich, D. Solomon, Windows Internals, 7th ed., Part 1, 2017 — Chapter 4: Thread scheduling.
  • Linux kernel documentation: sched-eevdf.rst, sched-design-CFS.rst, sched-deadline.rst, sched-ext.rst; the sched(7) man page.
  • I. Stoica, H. Abdel-Wahab, “Earliest Eligible Virtual Deadline First”, 1995; C. L. Liu, J. W. Layland, “Scheduling Algorithms for Multiprogramming in a Hard-Real-Time Environment”, JACM, 1973; C. A. Waldspurger, W. E. Weihl, “Lottery Scheduling”, OSDI 1994.

The End

Chapter 5: CPU Scheduling
Previous: Chapter 4: Threads · Next: Chapter 6: Synchronization
1 / 1