Knowledge / Computer Science and Engineering / CPU scheduling

CPU scheduling

Computer Science and Engineering (B.E./B.Tech) · Operating systems · free study notes for exams, placements and interviews

In short

CPU scheduling decides which ready process or thread gets the single CPU (or core) next, and for how long, so that the processor is never idle while work is waiting. It is the part of the operating system that turns a list of runnable jobs into an execution order, and that order decides whether a system feels fast, fair or starved. The topic is examined in every university OS paper (Gantt charts, average waiting and turnaround time) and is a guaranteed GATE CS question, usually numerical. In interviews it appears in systems roles at Qualcomm, Samsung R&D, Intel, Texas Instruments, Bosch, Tata Elxsi, KPIT, Nvidia, VMware, Oracle, Amazon and Microsoft IDC, and in every embedded or RTOS job where deadlines, not averages, are the requirement.

Explained

Fundamentals: bursts, schedulers and the dispatcher

Scheduling criteria

The classical algorithms

Real-time and multiprocessor scheduling

Schedulers in real systems

Key points

Formulas and examples

Turnaround time, TAT = Completion time (CT) − Arrival time (AT) — units: ms or s
Waiting time, WT = TAT − Burst time (BT) = TAT − CPU service time
Response time, RT = time of first dispatch − AT
Averages: Avg X = (ΣXi)/n for n processes
Throughput = number of processes completed / total time; CPU utilisation = busy time / total time × 100%
Degree of multiprogramming: CPU utilisation = 1 − pn, where p = fraction of time a process waits on I/O, n = processes in memory
Burst prediction: τ(n+1) = α·t(n) + (1−α)·τ(n); 0 ≤ α ≤ 1, t = actual last burst, τ = predicted burst
HRRN response ratio = (W + S)/S, W = waiting time so far, S = estimated service time
Round robin with context-switch cost s: useful CPU fraction = q/(q + s); worst wait for next turn = (n−1)(q + s)
RMS schedulability (sufficient): U = Σ(Ci/Ti) ≤ n(21/n − 1); EDF (necessary and sufficient, uniprocessor): U ≤ 1
CFS: vruntime += Δt × (1024 / weight_of_task); timeslice_i = sched_period × w_i / Σw

Example 1 — four algorithms on one workload. P1(AT 0, BT 7), P2(AT 2, BT 4), P3(AT 4, BT 1), P4(AT 5, BT 4).

FCFS Gantt chart:

| P1   0-7 | P2  7-11 | P3 11-12 | P4 12-16 |

CT = 7, 11, 12, 16 → TAT = 7, 9, 8, 11 (avg 35/4 = 8.75); WT = 0, 5, 7, 7 (avg 19/4 = 4.75).

SJF (non-preemptive): at t = 7 the ready set is P2(4), P3(1), P4(4) → P3 first, P2 before P4 on tie-break by arrival.

| P1 0-7 | P3 7-8 | P2 8-12 | P4 12-16 |

TAT = 7, 10, 4, 11 (avg 32/4 = 8.00); WT = 0, 6, 3, 7 (avg 16/4 = 4.00).

SRTF (preemptive): P2 preempts P1 at t = 2, P3 preempts P2 at t = 4.

| P1 0-2 | P2 2-4 | P3 4-5 | P2 5-7 | P4 7-11 | P1 11-16 |

CT: P1 16, P2 7, P3 5, P4 11 → TAT = 16, 5, 1, 6 (avg 28/4 = 7.00); WT = 9, 1, 0, 2 (avg 12/4 = 3.00) — the minimum possible.

Round Robin, q = 2 (new arrival enters the queue before the preempted process):

|P1 0-2|P2 2-4|P1 4-6|P3 6-7|P2 7-9|P4 9-11|P1 11-13|P4 13-15|P1 15-16|

CT: P1 16, P2 9, P3 7, P4 15 → TAT = 16, 7, 3, 10 (avg 36/4 = 9.00); WT = 9, 3, 2, 6 (avg 20/4 = 5.00); 9 slices → 8 inter-process context switches. Response times: P1 0, P2 0, P3 2, P4 4 — none above (n−1)q = 6, which is RR's real strength.

AlgorithmAvg TAT (ms)Avg WT (ms)PreemptiveStarvation
FCFS8.754.75NoNo
SJF8.004.00NoYes
SRTF7.003.00YesYes
RR (q = 2)9.005.00YesNo

Example 2 — priority scheduling (smaller number = higher priority). P1(AT 0, BT 4, pr 3), P2(AT 1, BT 3, pr 1), P3(AT 2, BT 1, pr 4), P4(AT 3, BT 5, pr 2).

Example 3 — burst prediction. τ1 = 10 ms, α = 0.5, actual bursts 6, 4, 6, 4. τ2 = 0.5(6) + 0.5(10) = 8; τ3 = 0.5(4) + 0.5(8) = 6; τ4 = 0.5(6) + 0.5(6) = 6; τ5 = 0.5(4) + 0.5(6) = 5 ms. With α = 0 the prediction never changes; with α = 1 only the last burst counts.

Example 4 — quantum, overhead and utilisation. Context switch s = 0.5 ms, 10 identical processes.

Example 5 — real-time schedulability. Tasks T1(C = 1, T = 4), T2(C = 2, T = 6), T3(C = 1, T = 8).

Example 6 — Linux nice weights. Task A at nice 0 (weight 1024) and task B at nice 5 (weight 335) on one core: A gets 1024/1359 = 75.3%, B gets 335/1359 = 24.7%, a ratio of 3.06 ≈ 1.255. With sched_period = 6 ms, A's slice ≈ 4.5 ms and B's ≈ 1.5 ms.

Where it is used

Interview questions

Common mistakes

Related topics

Open in the Knowledge reader →Find jobs →