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.
#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.
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.
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.
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.
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.