Skip to main content

3.3 Shortest Job First (SJF) & Shortest Remaining Time First (SRTF)

πŸ“šModule 03: CPU Scheduling AlgorithmsTopic 3.3⏱️14 min read
🎯High-Yield For:Computer Science Foundations β€’ Systems Engineering β€’ Technical Interviews

πŸ’‘ Core Intuition​

🍳 The Everyday Analogy: Hospital Emergency Room Triage​

Imagine an emergency outpatient department staffed by one trauma surgeon (the CPU) with patients of varying treatment complexity:

Architecture Flow

The Emergency Triage Analogy Pipeline

Mapping surgical triage strategies to SJF and SRTF CPU scheduling

πŸ’‘ Hover or click any card for deep-dive operational details
πŸ₯Admission

Triage Assessment

Ready Queue Arrival

Patients arrive with varying estimated procedure times.

β†’
Triage Decision
⏱️SJF Policy

Shortest Case First

Non-Preemptive SJF

The surgeon completes the current surgery, then takes the quickest case.

β†’
Preemption Check
⚑SRTF Policy

Emergency Interrupt

Preemptive SRTF

Surgeon pauses an ongoing procedure if an even quicker emergency arrives.

  • Non-Preemptive SJF: Once the surgeon starts a procedure, they finish it to completion. When selecting the next patient, they always choose the one with the shortest expected duration.
  • Preemptive SRTF: If a patient arrives whose procedure takes less time than the surgeon's remaining time on the current patient, the surgeon halts the current procedure and treats the new arrival immediately.

πŸ’» Bridging to Computer Science​

In operating systems, Shortest Job First (SJF) selects the process from the Ready Queue that has the smallest CPU burst time requirement.

  • If two processes have identical burst times, FCFS order is used to break the tie.
  • SJF exists in two distinct execution modes:
    1. Non-Preemptive SJF: The running process retains the CPU until it voluntarily releases it.
    2. Preemptive SJF (Shortest Remaining Time First - SRTF): A newly arriving process with a shorter remaining burst time immediately preempts the currently running process.


πŸ“š Core Deep-Dive & Concepts​

1. Non-Preemptive SJF Architectural Principles​

Definition: Whenever the scheduler makes a decision of selecting the next process for CPU execution among all available processes, the CPU is assigned to the process having the smaller burst time requirement. If there is a tie, FCFS is used to break the tie.

Non-Preemptive SJF vs. Preemptive SRTF

Contrasting cooperative completion against dynamic preemption

Non-Preemptive

Shortest Job First (SJF)

🀝
Dominant Architecture / DomainCooperative Short-Burst Priority
  • β€’Scheduling Trigger: Evaluated strictly when the running process terminates or blocks for I/O.
  • β€’Zero Preemption Overhead: A process is never context-switched out against its will.
  • β€’Tie-Breaking Rule: When burst times are equal, FCFS arrival order decides.
  • β€’Convoy Protection: Prevents small tasks from getting trapped behind new large arrivals.
"CPU assigned to the process having smaller burst time requirement among available processes."
Preemptive (Optimal)

Shortest Remaining Time First (SRTF)

⚑
Dominant Architecture / DomainMathematically Optimal Waiting Time
  • β€’Scheduling Trigger: Evaluated on process arrival, timer ticks, and state changes.
  • β€’Dynamic Preemption: A running process is evicted if a newcomer has a shorter remaining burst.
  • β€’Provably Optimal: Mathematically guarantees the minimal average waiting time for any given set of processes.
  • β€’Starvation Risk: Long-burst processes can starve indefinitely if shorter jobs continuously arrive.
"SRTF is called optimal as it guarantees minimal average waiting time."

2. Comprehensive Problem: Non-Preemptive SJF Step-by-Step Derivation​

Consider five processes arriving with the following arrival and burst times:

Process IDArrival Time (ATAT)Burst Time (BTBT)
P0P_01Β ms1\text{ ms}7Β ms7\text{ ms}
P1P_12Β ms2\text{ ms}5Β ms5\text{ ms}
P2P_23Β ms3\text{ ms}1Β ms1\text{ ms}
P3P_34Β ms4\text{ ms}2Β ms2\text{ ms}
P4P_45Β ms5\text{ ms}8Β ms8\text{ ms}

Execution Walkthrough:​

  • [0,1][0, 1]: No process has arrived. The CPU sits idle for 1Β ms1\text{ ms}.
  • At t=1t = 1: Only P0P_0 is available (AT=1,BT=7AT = 1, BT = 7). Since no other process is in the queue, P0P_0 is scheduled. Because scheduling is non-preemptive, P0P_0 runs uninterrupted from t=1β†’8t = 1 \to 8.
  • At t=8t = 8: P0P_0 completes. During [1,8][1, 8], all remaining processes (P1,P2,P3,P4P_1, P_2, P_3, P_4) have entered the Ready Queue:
    • P1:BT=5P_1: BT = 5
    • P2:BT=1P_2: BT = 1
    • P3:BT=2P_3: BT = 2
    • P4:BT=8P_4: BT = 8
    • Smallest burst is P2P_2 (BT=1BT = 1). P2P_2 executes from t=8β†’9t = 8 \to 9.
  • At t=9t = 9: P2P_2 completes. Remaining: P3(2),P1(5),P4(8)P_3(2), P_1(5), P_4(8). Smallest is P3P_3 (BT=2BT = 2). P3P_3 executes from t=9β†’11t = 9 \to 11.
  • At t=11t = 11: P3P_3 completes. Remaining: P1(5),P4(8)P_1(5), P_4(8). Smallest is P1P_1 (BT=5BT = 5). P1P_1 executes from t=11β†’16t = 11 \to 16.
  • At t=16t = 16: P1P_1 completes. Only P4P_4 remains (BT=8BT = 8). P4P_4 executes from t=16β†’24t = 16 \to 24.
  • At t=24t = 24: All processes finished.
⏱️

Non-Preemptive SJF Execution Gantt Chart

Timeline showing initial CPU idle gap and non-preemptive burst selection

IDLEIdle: 1ms
P0Burst: 7ms
P2Burst: 1ms
P3Burst: 2ms
P1Burst: 5ms
P4Burst: 8ms
01
8
9
11
16
24
Avg Turnaround10.6 ms
Avg Waiting Time6.0 ms
Throughput0.208 jobs/ms
CPU Utilization95.83%

Tabulation of Non-Preemptive SJF Metrics:​

ProcessArrival (ATAT)Burst (BTBT)Completion (CTCT)Turnaround (TAT=CTβˆ’ATTAT = CT - AT)Waiting (WT=TATβˆ’BTWT = TAT - BT)Response (RTRT)
P0P_01177888βˆ’1=78 - 1 = \mathbf{7}7βˆ’7=07 - 7 = \mathbf{0}1βˆ’1=01 - 1 = \mathbf{0}
P1P_12255161616βˆ’2=1416 - 2 = \mathbf{14}14βˆ’5=914 - 5 = \mathbf{9}11βˆ’2=911 - 2 = \mathbf{9}
P2P_23311999βˆ’3=69 - 3 = \mathbf{6}6βˆ’1=56 - 1 = \mathbf{5}8βˆ’3=58 - 3 = \mathbf{5}
P3P_34422111111βˆ’4=711 - 4 = \mathbf{7}7βˆ’2=57 - 2 = \mathbf{5}9βˆ’4=59 - 4 = \mathbf{5}
P4P_45588242424βˆ’5=1924 - 5 = \mathbf{19}19βˆ’8=1119 - 8 = \mathbf{11}16βˆ’5=1116 - 5 = \mathbf{11}

TATβ€Ύ=7+14+6+7+195=535=10.6Β ms\overline{TAT} = \frac{7 + 14 + 6 + 7 + 19}{5} = \frac{53}{5} = \mathbf{10.6\text{ ms}} WTβ€Ύ=0+9+5+5+115=305=6.0Β ms\overline{WT} = \frac{0 + 9 + 5 + 5 + 11}{5} = \frac{30}{5} = \mathbf{6.0\text{ ms}}


3. Preemptive SJF: Shortest Remaining Time First (SRTF)​

Definition: Shortest Remaining Time First (SRTF) is the preemptive version of Shortest Job First. It is called optimal because it mathematically guarantees the minimal average waiting time for any given set of processes.

Applying preemption to the exact same five processes:

Step-by-Step Chronological Execution:​

  • [0,1][0, 1]: CPU is idle.
  • At t=1t = 1: P0P_0 arrives (BT=7BT = 7). Starts execution.
  • At t=2t = 2: P1P_1 arrives (BT=5BT = 5).
    • P0P_0's remaining burst is 7βˆ’1=6Β ms7 - 1 = 6\text{ ms}.
    • Since P1P_1's burst (55) is less than P0P_0's remaining burst (66), P1P_1 preempts P0P_0!
    • P1P_1 begins executing (t=2β†’3t = 2 \to 3).
  • At t=3t = 3: P2P_2 arrives (BT=1BT = 1).
    • P1P_1's remaining burst is 5βˆ’1=4Β ms5 - 1 = 4\text{ ms}.
    • Since P2P_2's burst (11) is less than P1P_1's remaining burst (44), P2P_2 preempts P1P_1!
    • P2P_2 executes (t=3β†’4t = 3 \to 4).
  • At t=4t = 4: P2P_2 completes (CT=4CT = 4). P3P_3 arrives (BT=2BT = 2).
    • Active processes in Ready Queue: P3P_3 (BT=2BT = 2), P1P_1 (remaining =4= 4), P0P_0 (remaining =6= 6).
    • Smallest remaining is P3P_3 (22). P3P_3 executes (t=4β†’6t = 4 \to 6).
    • (Note: At t=5t = 5, P4P_4 arrives with BT=8BT = 8. Since P4P_4's burst 8>P38 > P_3's remaining burst 11, P3P_3 continues uninterrupted until t=6t = 6).
  • At t=6t = 6: P3P_3 completes (CT=6CT = 6).
    • Ready Queue: P1P_1 (remaining =4= 4), P0P_0 (remaining =6= 6), P4P_4 (remaining =8= 8).
    • Smallest remaining is P1P_1 (44). P1P_1 executes to completion (t=6β†’10t = 6 \to 10).
  • At t=10t = 10: P1P_1 completes (CT=10CT = 10).
    • Ready Queue: P0P_0 (remaining =6= 6), P4P_4 (remaining =8= 8).
    • Smallest remaining is P0P_0 (66). P0P_0 executes to completion (t=10β†’16t = 10 \to 16).
  • At t=16t = 16: P0P_0 completes (CT=16CT = 16).
    • Only P4P_4 remains (88). P4P_4 executes to completion (t=16β†’24t = 16 \to 24).
  • At t=24t = 24: All processes finished.
⏱️

Preemptive SRTF Execution Gantt Chart

Visualizing dynamic preemption points at t = 2 and t = 3 leading to optimal wait time

IDLEIdle: 1ms
P0P0 (run 1ms)
P1P1 (run 1ms)
P2P2 finishes
P3P3 finishes
P1P1 finishes
P0P0 finishes
P4P4 finishes
01
2
3
4
6
10
16
24
Avg Turnaround9.0 ms
Avg Waiting Time4.4 ms
Optimality Gain26.7% vs SJF
CPU Utilization95.83%

Tabulation of Preemptive SRTF Metrics:​

ProcessArrival (ATAT)Burst (BTBT)Completion (CTCT)Turnaround (TAT=CTβˆ’ATTAT = CT - AT)Waiting (WT=TATβˆ’BTWT = TAT - BT)Response (RTRT)
P0P_01177161616βˆ’1=1516 - 1 = \mathbf{15}15βˆ’7=815 - 7 = \mathbf{8}1βˆ’1=01 - 1 = \mathbf{0}
P1P_12255101010βˆ’2=810 - 2 = \mathbf{8}8βˆ’5=38 - 5 = \mathbf{3}2βˆ’2=02 - 2 = \mathbf{0}
P2P_23311444βˆ’3=14 - 3 = \mathbf{1}1βˆ’1=01 - 1 = \mathbf{0}3βˆ’3=03 - 3 = \mathbf{0}
P3P_34422666βˆ’4=26 - 4 = \mathbf{2}2βˆ’2=02 - 2 = \mathbf{0}4βˆ’4=04 - 4 = \mathbf{0}
P4P_45588242424βˆ’5=1924 - 5 = \mathbf{19}19βˆ’8=1119 - 8 = \mathbf{11}16βˆ’5=1116 - 5 = \mathbf{11}

TATβ€Ύ=15+8+1+2+195=455=9.0Β ms\overline{TAT} = \frac{15 + 8 + 1 + 2 + 19}{5} = \frac{45}{5} = \mathbf{9.0\text{ ms}} WTβ€Ύ=8+3+0+0+115=225=4.4Β ms\overline{WT} = \frac{8 + 3 + 0 + 0 + 11}{5} = \frac{22}{5} = \mathbf{4.4\text{ ms}}

Direct Metric Comparison on Identical Workload
  • Non-Preemptive SJF Average Waiting Time: 6.0Β ms6.0\text{ ms}
  • Preemptive SRTF Average Waiting Time: 4.4Β ms\mathbf{4.4\text{ ms}} (Optimal!)
  • SRTF slashed average waiting time by 26.7%26.7\% compared to non-preemptive SJF, and achieved an immediate response time of 0Β ms0\text{ ms} for four out of five processes.

4. Advantages, Disadvantages & Starvation​

Advantages of SRTF:​

  1. Guarantees Minimal Average Waiting Time: Provably optimal; provides the theoretical standard benchmark against which all other scheduling algorithms are evaluated.
  2. Superior Response Time: New short tasks receive immediate CPU access, drastically lowering initial response latency compared to FCFS.

Disadvantages of SRTF:​

  1. Unimplementable in Pure Practice: An operating system kernel cannot know the exact execution length of an upcoming CPU burst in advance.
  2. Indefinite Blocking (Starvation): Processes with large CPU burst requirements may starve indefinitely if a continuous stream of short-burst processes enters the Ready Queue.
  3. No Support for Priority: A mission-critical kernel daemon with a large burst will be starved by trivial user-space tasks with short bursts.
  4. Poor Response Time for Long Processes: Long-running computational processes suffer severe turnaround and response latency degradation.

5. Predicting Future CPU Bursts: Exponential Averaging​

Because the operating system cannot foresee how many milliseconds a process will compute before its next system call, practical systems estimate the next burst using Exponential Averaging (Exponential Smoothing):

Ο„n+1=Ξ±β‹…tn+(1βˆ’Ξ±)β‹…Ο„n\mathbf{\tau_{n+1} = \alpha \cdot t_n + (1 - \alpha) \cdot \tau_n}

Where:

  • tnt_n: Actual duration of the nthn^{\text{th}} (most recent) CPU burst.
  • Ο„n\tau_n: Predicted duration for the nthn^{\text{th}} CPU burst.
  • Ο„n+1\tau_{n+1}: Predicted duration for the upcoming (n+1)th(n+1)^{\text{th}} CPU burst.
  • Ξ±\alpha: Smoothing weighting factor (0≀α≀10 \le \alpha \le 1).

Exponential Smoothing Factor Boundary Analysis

Analyzing how the weighting parameter alpha governs scheduler memory

Pure History

Alpha = 0 (No Adaptation)

πŸ›οΈ
Dominant Architecture / Domaintau_{n+1} = tau_n
  • β€’Recent behavior is completely ignored: t_n has 0 weight.
  • β€’The predicted value never adapts to changing process phases.
  • β€’The forecast remains locked to the initial system default tau_0.
"Recent burst duration has no effect on upcoming predictions."
Pure Recency

Alpha = 1 (Zero History)

⚑
Dominant Architecture / Domaintau_{n+1} = t_n
  • β€’Past history is completely discarded: tau_n has 0 weight.
  • β€’The next burst is predicted to be exactly identical to the last observed burst.
  • β€’Highly volatile: susceptible to extreme noise from occasional brief I/O spikes.
"Only the most recent burst matters; prior historical trends are discarded."

Expanding the Historical Horizon:​

By repeated algebraic substitution: Ο„n+1=Ξ±tn+(1βˆ’Ξ±)Ξ±tnβˆ’1+(1βˆ’Ξ±)2Ξ±tnβˆ’2+β‹―+(1βˆ’Ξ±)jΞ±tnβˆ’j+β‹―+(1βˆ’Ξ±)n+1Ο„0\tau_{n+1} = \alpha t_n + (1 - \alpha)\alpha t_{n-1} + (1 - \alpha)^2 \alpha t_{n-2} + \dots + (1 - \alpha)^j \alpha t_{n-j} + \dots + (1 - \alpha)^{n+1} \tau_0

Because both Ξ±\alpha and (1βˆ’Ξ±)(1 - \alpha) are less than or equal to 11, each successive historical term (1βˆ’Ξ±)j(1 - \alpha)^j decreases exponentially. More recent bursts contribute heavily to the prediction, while ancient bursts fade into negligible background weight. In production implementations, Ξ±\alpha is commonly tuned to 0.50.5 to balance historical stability with responsiveness.


πŸ“ Architecture / Visual Blueprint​

The SRTF Preemption & State Migration Lifecycle​

The diagram below maps the runtime decision loop of the Short-Term Scheduler under SRTF when a new process arrives:

Architecture Flow

SRTF Dynamic Preemption & State Migration Engine

The kernel decision loop comparing newcomer burst against currently running remaining time

πŸ“₯Job Admission (Memory)
βš–οΈShort-Term Scheduler (Arbiter)
⚑Execution Core
1Interrupt Check
2If Shorter
3Switch Core
πŸ“₯Burst = BT_new
Process Arrival
Newcomer P_new
βš–οΈKernel Comparison
Remaining Time Test
BT_new < Remaining(P_curr)?
πŸ”„Save PCB State
Context Preemption
Evict P_curr to Queue
⚑CPU Granted
Dispatch Shortest
Execute P_new
πŸ’‘Click or hover any card or transition arrow above to inspect deep-dive operational mechanics

🏭 In The Real World: Production Case Study​

Distributed Stage Scheduling in Apache Spark & Database Query Planners​

While OS kernels cannot predict arbitrary user program execution lengths, specialized production systems leverage SJF/SRTF principles where job sizes are deterministic:

Distributed DAG Task Engine: Stage Prioritization

Applying Shortest Job First principles to distributed compute stages

Scheduled First (SJF)

Stage A: Index Scan (10 KB)

⚑
Dominant Architecture / DomainEstimated Execution: ~2ms
  • β€’Minimal Burst: Lightweight partition read finishes in single-digit milliseconds.
  • β€’Early Materialization: Frees intermediate memory buffers for downstream map tasks.
  • β€’Lowers Average Stage Latency: Dramatically cuts cluster-wide stage wait queues.
"Short stages are dispatched immediately to minimize overall DAG turnaround time."
Queued for Later

Stage B: Distributed Join (500 GB)

🐒
Dominant Architecture / DomainEstimated Execution: ~120s
  • β€’Heavy Compute Burst: Involves cluster-wide shuffle network serialization and disk spill.
  • β€’Deferred Execution: Runs in dedicated batch executor pool once quick queries clear.
  • β€’Prevents Convoy Blocking: Avoids stalling hundreds of concurrent ad-hoc analytics tasks.
"Heavy batch stages are deferred behind quick analytical jobs to protect interactive SLAs."

Production Implementations:​

  1. Database Query Optimizers (PostgreSQL / BigQuery): When executing multiple sub-queries, cost-based optimizers estimate cost using table row counts and index metadata. Independent short scans run first to materialize quick intermediate buffers, reducing lock holding times.
  2. Apache Spark Job Scheduling: In multi-tenant Spark clusters, the Fair Scheduler pools tasks. Short ad-hoc analytical queries are placed in high-priority queues with burst limits, ensuring quick business queries never sit starved behind massive 10-hour ETL batch jobs.

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions

Question 1: Prove why Shortest Remaining Time First (SRTF) is provably optimal with respect to average waiting time. Answer:

  1. Consider moving a shorter process ahead of a longer process in a non-preemptive schedule.
  2. If process PAP_A with burst tAt_A runs before process PBP_B with burst tBt_B (where tA<tBt_A < t_B), PAP_A finishes at tAt_A and PBP_B waits tAt_A. Total wait contributed =tA= t_A.
  3. If their order is reversed (PBP_B runs first), total wait contributed =tB= t_B.
  4. Since tA<tBt_A < t_B, scheduling the shorter job first decreases the waiting time of the subsequent job by (tBβˆ’tA)(t_B - t_A), while the shorter job finishes earlier.
  5. Under preemptive SRTF, this minimization is enforced continuously at every decision instant, yielding the absolute mathematical minimum for average waiting time.

Question 2: Explain the Starvation (Indefinite Blocking) problem in SJF/SRTF scheduling. What operational technique resolves it? Answer:

  1. Starvation: If short-burst processes arrive continuously in the Ready Queue, long-burst processes will repeatedly be passed over or preempted, remaining in the Ready Queue indefinitely without ever completing.
  2. Resolution via Aging: An operating system technique where the priority or virtual burst of an awaiting process is gradually adjusted over time. As a long process waits longer in the queue, its effective priority increases until it is guaranteed to execute.
Common Interview Traps
  • The "SRTF has Zero Response Time for All Jobs" Fallacy: SRTF yields fantastic response times only for short jobs. Long jobs that arrive early may be preempted multiple times and sit in the queue for minutes, accumulating massive response delays.
  • Assuming Preemption Occurs When Remaining Times are Equal: If running process PAP_A has a remaining burst of 3Β ms3\text{ ms} and newcomer PBP_B arrives with BT=3Β msBT = 3\text{ ms}, no preemption occurs. The running process keeps the CPU because PBP_B does not have a shorter remaining burst time. Context switching incurs overhead and is only justified when the newcomer strictly reduces remaining time.
  • Confusing Actual Burst (tnt_n) with Predicted Burst (Ο„n\tau_n) in Formulas: In exponential smoothing questions, always distinguish tnt_n (measured physical execution time) from Ο„n\tau_n (estimated model parameter).

πŸ’¬

Discussion & Doubts