What is Shortest Job First (SJF) scheduling?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Operating Systems development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Shortest Job First (SJF) selects the process with the smallest next CPU burst for scheduling. It is provably optimal for minimizing average waiting time when all processes are available simultaneously. Non-preemptive SJF: once CPU is given to a process, it runs until it completes or blocks — can't preempt even if a shorter job arrives. Example (non-preemptive): P1(6ms), P2(8ms), P3(7ms), P4(3ms) all arrive at t=0. Order: P4(3), P1(6), P3(7), P2(8). Gantt: |P4(0-3)|P1(3-9)|P3(9-16)|P2(16-24)|. Avg wait = (0+16+9+3)/4 = 7ms. Preemptive SJF (Shortest Remaining Time First — SRTF): if a new process arrives with a shorter remaining burst than the current running process, preempt and switch. Optimal average waiting time for all cases: Example: P1(7) arrives t=0, P2(4) arrives t=2. At t=2, P2 has 4ms remaining, P1 has 5ms remaining → switch to P2. |P1(0-2)|P2(2-6)|P1(6-11)|. Problems: (1) Starvation: if short jobs keep arriving, long jobs may never run; (2) Predicting burst time: impossible to know the exact future CPU burst. In practice, exponential averaging of past bursts: estimated_next = α × actual_prev + (1-α) × estimated_prev. Starvation solution: aging — gradually increase priority of waiting processes over time.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Operating Systems answers easy to follow.