A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Shortest Job First (SJF) CPU Scheduling

College Depth 101 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
365prerequisites beneath it
See this on the map →
CPU Scheduling FundamentalsProcessor Affinity and CPU Binding→→Multilevel Feedback Queue (MLFQ) Scheduling
scheduling algorithms cpu

Core Idea

SJF scheduling selects the process with the shortest expected burst time to minimize average waiting time. This algorithm is provably optimal for non-preemptive scheduling but requires accurate knowledge of future burst times, which is impractical in real systems. Preemptive SJF (Shortest Remaining Time First) can outperform non-preemptive SJF but adds complexity.

How It's Best Learned

Study the algorithm with concrete examples showing scheduling timelines. Compare average waiting times against FCFS and round-robin. Implement a simple SJF simulator to see how different burst time predictions affect scheduling.

Common Misconceptions

Explainer

From CPU scheduling basics, you know the OS must choose which ready process gets the CPU next, and that different policies produce different average wait times. Shortest Job First (SJF) takes a greedy approach: always run the process with the shortest expected CPU burst next. The intuition is the same as choosing the fastest checkout line at a grocery store — letting the person with one item go first minimizes total waiting for everyone behind them.

To see why SJF is optimal for minimizing average waiting time, consider a simple example. Three processes arrive simultaneously with burst times of 6, 8, and 2 milliseconds. Under FCFS (in arrival order), waiting times are 0, 6, and 14 — average 6.67ms. Under SJF, the order is 2, 6, 8, giving waiting times of 0, 2, and 8 — average 3.33ms. SJF always wins because short jobs finish quickly and stop contributing to everyone else's wait. This can be proven formally: any schedule that does not run the shortest remaining job can be improved by swapping it earlier, which always reduces or maintains total waiting time.

The catch is that SJF requires knowing how long each process will run before it runs — and the OS cannot predict the future. In practice, systems estimate burst times using historical data: if a process's last three CPU bursts were 5ms, 6ms, and 4ms, the next is likely around 5ms. Exponential averaging is the classic technique, where recent bursts count more than older ones. These estimates are imperfect, but they make SJF approximately usable.

Preemptive SJF, also called Shortest Remaining Time First (SRTF), goes further: if a new process arrives with a shorter burst than the time remaining on the currently running process, the OS preempts the running process immediately. This achieves even lower average waiting time but adds context-switch overhead and introduces a serious fairness problem — starvation. A long-running process can be perpetually pushed aside as short jobs keep arriving. This is the fundamental tradeoff: SJF is optimal for throughput and wait times but fundamentally unfair to long jobs, which is why real schedulers rarely use pure SJF and instead incorporate aging mechanisms or multi-level feedback queues to balance efficiency with fairness.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Process Model Formalization → Context Switching and CPU Dispatch → CPU Scheduling Fundamentals → Processor Affinity and CPU Binding → Shortest Job First (SJF) CPU Scheduling

Longest path: 102 steps · 365 total prerequisite topics

Prerequisites (2)

Leads To (1)