Highlights

Contents

Part VI
06

CPU scheduling

The scheduler decides which ready work gets a core and when a timer can take it back. A schedule can improve response while worsening completion or context-switch overhead. We will compute FCFS, SJF, SRTF and round-robin timelines, then add preemption, multicore queues and cache affinity.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1OS fundamentals2Processes3Fork, exec and threads4Synchronization5Deadlocks6CPU scheduling7Paging and memory8Copy-on-write9Filesystems10I/O and IPC11Protection and Linux12Complete server trace

#Section 22Scheduling metrics

Three jobs arrive at times 0, 1 and 2 with bursts 5, 3 and 1. Completion time is when a job finishes. Turnaround time is completion minus arrival. Waiting time is turnaround minus burst. Response time is first run minus arrival. These metrics answer different questions, so one average cannot describe the whole schedule.

For FCFS, A completes at 5, B at 8 and C at 9. B has turnaround 7 and waiting 4, while C has turnaround 7 and waiting 6. The mean waiting time is 3.333333. Those values come from the supplied trace, not a rounded diagram estimate.

FCFS: B ARRIVES AT 1, STARTS AT 5, FINISHES AT 8FCFS: B ARRIVES AT 1, STARTS AT 5, FINISHES AT 8ABC0123456789msA arrivesB arrivesC arriveswaiting = 7 - 3 = 4turnaround = 8 - 1 = 7response = first run - arrival = 4
Figure 3.27Illustrative FCFS timeline keeps completion, turnaround, waiting and response distinct. Orange marks waiting because it is time ready work loses while another job runs.

Response matters to an interactive server, while turnaround matters to a batch job. A scheduler that optimizes one can worsen another. Always draw the timeline first, then compute each row from its own arrival and burst.

Response ends at first service; waiting can continue afterward under preemption. Equal values in a non-preemptive example do not make them the same metric.

The timeline isolates one-core scheduling without I/O or switching cost. It is not measured admissions latency. Define whether a stopwatch begins at readiness, queue insertion, or execution.

#Section 23FCFS, SJF and SRTF

First-come, first-served runs A from 0 to 5, B from 5 to 8 and C from 8 to 9. It is simple and non-preemptive, but a long A creates a convoy for shorter jobs. Shortest-job-first runs A, then C from 5 to 6, then B from 6 to 9 because C is shorter among the jobs available after A.

Shortest-remaining-time-first can preempt. A starts at 0, B arrives at 1 with 3 units against A’s remaining 4, and C arrives at 2 with 1. The exact trace is A, B, C, B, A over intervals 0 to 1, 1 to 2, 2 to 3, 3 to 5 and 5 to 9. Its mean waiting time is 1.666667.

FCFS: THE FIRST LONG BURST MAKES LATER JOBS WAITFCFS: THE FIRST LONG BURST MAKES LATER JOBS WAITCPUABC0123456789Await 0 msBwait 4 msCwait 6 msthin span: lifetime; thick span: time actually on the core
Figure 3.28Illustrative FCFS exposes convoy waiting in the A, B, C trace. Orange marks A because its long first burst delays both later jobs.
SJF: PICK THE SHORTEST AVAILABLE JOB AFTER A FINISHESSJF: PICK THE SHORTEST AVAILABLE JOB AFTER A FINISHESCPUACB0123456789Await 0 msBwait 5 msCwait 3 msthin span: lifetime; thick span: time actually on the core
Figure 3.29Illustrative SJF chooses C after A completes. Orange marks C because the shortest available burst finishes first.
SRTF: SHORTER REMAINING WORK PREEMPTS THE CURRENT JOBSRTF: SHORTER REMAINING WORK PREEMPTS THE CURRENT JOBCPUABCBA0123456789Await 4 msBwait 1 msCwait 0 msthin span: lifetime; thick span: time actually on the core
Figure 3.30Illustrative SRTF preempts as shorter jobs arrive. Orange marks C’s one-unit interval because it is the shortest remaining work.

The price of preemption is extra context switching and state management. SJF also needs a burst estimate, which real systems infer rather than know exactly.

#Section 24Round robin and preemption

Round robin gives each ready job a time quantum of 2 ms. The exact trace is A from 0 to 2, B from 2 to 4, C from 4 to 5, A from 5 to 7, B from 7 to 8 and A from 8 to 9. A completes at 9, B at 8 and C at 5. Their waiting times are 4, 4 and 2, with mean 3.333333.

If the quantum is too small, timer interrupts and context switches consume more CPU. If it is too large, the schedule approaches FCFS and response worsens. Preemption depends on a timer or another kernel event that returns control from user code. Without it, a process could retain a core indefinitely.

ROUND ROBIN: THE TWO MILLISECOND QUANTUM ROTATES THE COREROUND ROBIN: THE TWO MILLISECOND QUANTUM ROTATES THE CORECPUABCABA0123456789Await 4 msBwait 4 msCwait 2 msthin span: lifetime; thick span: time actually on the core
Figure 3.31Illustrative round-robin timeline uses the declared 2 ms quantum and exact completion order. Orange marks the first slice because it establishes the rotation.

Priority scheduling adds another starvation risk. Aging raises a long-waiting job’s priority, while multilevel feedback queues change a task’s queue based on observed CPU behavior.

Priority and multilevel scheduling

A priority scheduler selects according to an assigned rank, which may reflect urgency or policy. Preemptive priority can stop a lower-ranked task when urgent work becomes runnable; non-preemptive priority waits for the current burst to end. Either needs a rule for ties and starvation. Aging adjusts waiting tasks so continuously arriving high-priority work does not exclude them forever.

Multilevel queues classify tasks; feedback queues can change class from observed behavior. Priority inversion is separate: urgent work waits on a resource held by less urgent work.

#Section 25Multicore scheduling

On a symmetric multiprocessor, each core can run a ready thread. A global queue simplifies balancing but increases contention. Per-CPU queues reduce queue contention and preserve cache locality, but one core can become idle while another has work. Load balancing migrates tasks and pays for lost cache affinity.

Affinity pins a task or process to selected CPUs. It helps when cache-hot state matters, but hard pinning can strand capacity. A scheduler therefore balances utilization, fairness, response and locality instead of maximizing one value.

A TIMER ENDS EACH SLICE; MIGRATION BALANCES CORES BUT LEAVES A WARM CACHE BEHINDA TIMER ENDS EACH SLICE; MIGRATION BALANCES CORES BUT LEAVES A WARM CACHE BEHINDtimer tickcore 0run queueABCDL1/L2 cachecore 1run queueEL1/L2 cachemigrate Dcore 1 was idle, but D'scache lines stay on core 0Daffinity keeps a task on its warm core unless the imbalance is worth the cold start
Figure 3.32Illustrative timer preemption, SMP execution, affinity and migration form separate mechanisms. Orange marks migration because it trades balance for locality.

Finch has 20 ready connections and 2 cores, so two worker threads could run two independent jobs while eighteen wait, if each ready connection has supplied one queued job. The count says nothing about how long each callback runs, which is why event-loop callbacks must remain bounded.