OPERATING SYSTEMS/Student Engineering Notebook
Page 09·notes
CPU SCHEDULING ALGORITHMS—~22 MIN READ

SJF, SRTF & Priority Scheduling: Optimal Turnaround & Starvation

“Non-preemptive SJF, preemptive SRTF (optimal waiting time), Priority scheduling, and Aging.”

1.Shortest Job First (SJF): Principle & Theoretical Optimality

Shortest Job First (SJF) associates with each process the length of its next CPU burst. When the CPU becomes available, it is assigned to the process that has the smallest burst time. If two processes have identical burst lengths, FCFS scheduling is used as a tiebreaker.

Provable Optimality: SJF is provably optimal in giving minimum average waiting time for a given set of stationary processes. Moving a short process before a long one reduces the waiting time of the short process by more than it increases the waiting time of the long one.

The Fundamental Limitation: SJF cannot be implemented in general-purpose long-term/short-term schedulers because the Operating System cannot predict the exact future CPU burst length of a user process.

Burst Prediction via Exponential Smoothing: The OS approximates the next burst \tau_{n+1} using historical actual bursts t_n and prior estimates \tau_n: \tau_{n+1} = \alpha \cdot t_n + (1 - \alpha) \cdot \tau_n, where 0 ≤ \alpha ≤ 1 (typically \alpha = 0.5).

notice this:— GATE Trap: Provable Optimality Scope

In GATE multiple-choice questions, remember: Non-preemptive SJF is optimal among non-preemptive algorithms, whereas Preemptive SJF (SRTF) is optimal among ALL scheduling algorithms for minimizing average waiting time.

2.Shortest Remaining Time First (SRTF): Preemptive Scheduling

SRTF is the preemptive version of SJF. When a new process arrives at the Ready Queue with a remaining CPU burst time strictly shorter than the currently running process's remaining time, the running process is preempted and moved back to the Ready Queue.

Key GATE Preemption Condition: Preemption occurs IF AND ONLY IF Remaining_Time(New) < Remaining_Time(Current). If Remaining_Time(New) Remaining_Time(Current)==, the CPU continues running the current process to avoid unnecessary context-switch overhead.

Trade-off: While SRTF yields the lowest theoretical average waiting time, every preemption incurs a CPU context switch. In practice, high context-switch frequency degrades effective throughput.

3. Algorithm Comparison Matrix

Detailed algorithmic trade-offs across preemption, optimality, starvation risk, and typical use cases:

Feature / AspectAlgorithmPreemptive?
FCFSNoFairness / Simplicity
SJF (Non-preemptive)NoAvg WT (Non-preemptive)
SRTF (Preemptive)YesAvg WT (Overall)
Round Robin (RR)Yes (Time Quantum)Response Time
Priority SchedulingBoth (Preemptive & Non)Importance / Urgency
WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: In SRTF, whenever a new process arrives, re-evaluate remaining burst times of all active processes immediately. Preempted processes retain their remaining burst times and return to the ready queue.

4. GATE Step-by-Step Numerical: SRTF Gantt Chart & Turnaround Time

Problem Statement:
Consider four processes P1, P2, P3, and P4 arriving with the following Arrival Times (AT) and CPU Burst Times (BT): P1(AT=0, BT=8), P2(AT=1, BT=4), P3(AT=2, BT=9), and P4(AT=3, BT=5). If Preemptive Shortest Remaining Time First (SRTF) scheduling is used, trace the Gantt chart and compute the Average Turnaround Time and Average Waiting Time.
P1AT = 0, BT = 8P2AT = 1, BT = 4P3AT = 2, BT = 9P4AT = 3, BT = 5
Step 1: Gantt Chart Execution Trace (t = 0 to t = 5)

• At t = 0: Only P1 is available. P1 executes from t = 0 to 1. • At t = 1: P2 arrives (BT = 4). P1 has remaining burst = 7. Since 4 < 7, P1 is preempted. P2 takes CPU. • At t = 2: P3 arrives (BT = 9). P2 has remaining burst = 3. Since 3 < 9, P2 continues. • At t = 3: P4 arrives (BT = 5). P2 has remaining burst = 2. Since 2 < 5, P2 continues. • At t = 5: P2 completes execution! Completion Time CT(P2) = 5.

t=0..1: P1 | t=1..5: P2 (CT = 5)
↳ Result: CT(P2) = 5
Step 2: Remaining Queue Evaluation (t = 5 to t = 26)

• At t = 5: Ready queue holds P1(rem = 7), P4(rem = 5), P3(rem = 9). Smallest remaining burst is P4 (5). P4 executes from t = 5 to 10. CT(P4) = 10. • At t = 10: Ready queue holds P1(rem = 7) and P3(rem = 9). Smallest is P1 (7). P1 executes from t = 10 to 17. CT(P1) = 17. • At t = 17: Only P3 remains (BT = 9). P3 executes from t = 17 to 26. CT(P3) = 26.

t=5..10: P4 | t=10..17: P1 | t=17..26: P3
↳ Result: CT(P4) = 10, CT(P1) = 17, CT(P3) = 26
Step 3: Calculate Turnaround Time (TAT = CT - AT) and Waiting Time (WT = TAT - BT)

• P1: TAT = 17 - 0 = 17, WT = 17 - 8 = 9 • P2: TAT = 5 - 1 = 4, WT = 4 - 4 = 0 • P3: TAT = 26 - 2 = 24, WT = 24 - 9 = 15 • P4: TAT = 10 - 3 = 7, WT = 7 - 5 = 2 Total TAT = 17 + 4 + 24 + 7 = 52. Average TAT = 52 / 4 = 13.0 units. Total WT = 9 + 0 + 15 + 2 = 26. Average WT = 26 / 4 = 6.5 units.

Avg TAT = 52 / 4 = 13.0 | Avg WT = 26 / 4 = 6.5
↳ Result: Avg TAT = 13.0 units, Avg WT = 6.5 units
Final Answer:Average Turnaround Time = 13.0 units, Average Waiting Time = 6.5 units

5.Priority Scheduling & The Aging Mitigation Strategy

In Priority Scheduling, an integer priority is assigned to each process. By standard UNIX convention, smaller integer values indicate higher priority (e.g., priority 0 is higher than priority 10). The CPU is allocated to the process with the highest priority.

Starvation (Indefinite Blocking): A major hazard where low-priority processes may wait indefinitely if there is a steady stream of higher-priority processes.

The Solution — Aging: Aging is a technique of gradually increasing the priority of processes that wait in the system for a long time. For example, if priorities range from 127 (lowest) to 0 (highest), the kernel can decrement the priority value by 1 every 15 minutes of ready-queue waiting. Eventually, even the lowest-priority process climbs to the top.

RAPID REVIEW · NOTEBOOK SUMMARY

6. 1-Minute Panic Revision

core principle:

“SJF minimizes average waiting time (provably optimal). SRTF is preemptive SJF. Priority scheduling requires aging to prevent starvation.”

must-remember principles:
  • •Exponential Smoothing: tau_(n+1) = alpha t_n + (1 - alpha) tau_n.
  • •TAT = CT - AT; WT = TAT - BT.
  • •SRTF re-evaluates scheduling whenever a new process arrives with remaining burst < current process.
critical exam pitfalls:
  • •SJF is provably optimal for average waiting time among all non-preemptive algorithms, but cannot be implemented without predicting future burst time.
  • •Priority Inversion: High priority process blocked waiting for resource held by low priority process. Fixed by Priority Inheritance Protocol (PIP).
decision flow & invariants:
[ SRTF PREEMPTION DECISION TREE ] New Process P_new arrives at time t: │ ▼ Is Remaining_BT(P_new) < Remaining_BT(P_current) ? │ ├─ Yes ───> [ PREEMPT P_current ] │ Save context, move P_current to Ready Queue │ Dispatch P_new to CPU │ └─ No ───> [ KEEP RUNNING P_current ] Insert P_new into Ready Queue ordered by remaining BT
SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Primary Academic Texts
Operating System Concepts (10th Edition)↗by Silberschatz, Galvin, Gagne · Chapter 5: CPU Scheduling (Section 5.3.2: SJF & Section 5.3.3: Priority Scheduling)

Official slides covering mathematical proof of SJF optimality, exponential smoothing, and priority aging.

ACADEMIC STANDARD
Operating Systems: Three Easy Pieces (OSTEP)↗by Remzi H. Arpaci-Dusseau & Andrea C. Arpaci-Dusseau · Chapter 7: Scheduling: Introduction (SJF & STCF / Preemptive SJF)

Clear proof of why Shortest Time-to-Completion First (STCF/SRTF) minimizes average turnaround time.

ACADEMIC STANDARD
ALTERNATIVE PEDAGOGICAL PERSPECTIVES

If the textbook derivation did not click immediately, consult these targeted video lessons:

Shortest Remaining Time First (SJF With Preemption) with ExampleGate Smashers (Varun Singla) (15 mins)

Step-by-step Gantt chart solving with detailed calculations of CT, TAT, and WT under arrival preemption.

Watch Lesson↗
Pre-emptive Priority Scheduling Algorithm with ExampleGate Smashers (Varun Singla) (14 mins)

Covers high-yield GATE traps: handling ties, priority inversion, and using aging to eliminate starvation.

Watch Lesson↗
RECOMMENDED YOUTUBE LECTURES & EDUCATORS: MODULE 3: CPU SCHEDULING ALGORITHMS

Curated lectures on Gantt Charts, Preemption, FCFS Convoy Effect, Round Robin Quantum & SJF/SRTF:

Gate Smashers · L-2.3: First Come First Serve (FCFS) CPU Scheduling↗Instructor: Varun Singla · Covers: FCFS with Arrival Times, Convoy Effect illustration, Turnaround Time and Waiting Time

Step-by-step Gantt chart calculations with zero arithmetic ambiguity.

Open YouTube↗
Gate Smashers · L-2.7: Round Robin (RR) Scheduling Algorithm with Example↗Instructor: Varun Singla · Covers: Preemptive Ready Queue queue management, Time Quantum tuning, Context Switch overhead

The most reliable demonstration of ready-queue tracking during simultaneous arrivals.

Open YouTube↗
Neso Academy · CPU Scheduling Criteria & Algorithms Playlist↗Instructor: Neso Academy · Covers: SJF vs SRTF preemption tie-breakers, Priority Scheduling with Aging, Multi-Level Feedback Queues

Structured textbook-level coverage with cleanly paced whiteboard walkthroughs.

Open YouTube↗
Knowledge Gate · CPU Scheduling GATE PYQs & Edge Cases↗Instructor: Sanchit Jain · Covers: GATE tricky problems with I/O burst interleaving, non-zero arrival times, priority ties

Intense numerical speed practice and identifying examiner traps in Gantt charts.

Open YouTube↗