First come first served and shortest job first
About this lesson
When several jobs are ready and there is one CPU, the scheduler decides who runs next. Follow three jobs, A needing 100 seconds and B and C needing 10, through two classic policies. First come first served runs them in arrival order and leaves the short jobs stuck behind the long one: the convoy effect. Shortest job first runs the short ones first and cuts the average turnaround from 110 to 50 seconds, until jobs arrive at different times and its limits show. The lesson on turnaround, response time and fairness defines the measures used here, and The context switch and the timer interrupt explains how a kernel can stop a running job.
Three jobs, one CPU
Three jobs wait in the ready queue, the list of jobs ready to run. A needs 100 seconds of CPU time; B and C need 10 each. All arrive at time 0. The scheduler, part of the kernel, picks who runs next.
job arrives needs (run time) A 0 100 s B 0 10 s C 0 10 s one CPU: one job runs at a time turnaround = time finished - time arrived (defined in the lesson on turnaround, response time and fairness)
Read the lesson step by step
Three jobs, one CPU
Three jobs wait in the ready queue, the list of jobs ready to run. A needs 100 seconds of CPU time; B and C need 10 each. All arrive at time 0. The scheduler, part of the kernel, picks who runs next.
job arrives needs (run time) A 0 100 s B 0 10 s C 0 10 s one CPU: one job runs at a time turnaround = time finished - time arrived (defined in the lesson on turnaround, response time and fairness)First come, first served
First come first served (FCFS, also called FIFO) runs jobs in the order they arrived. Each job runs to the end before the next one starts. A, B and C arrived together in that order, so A goes first.
FCFS: take the job at the front of the queue, run it until it is done, repeat ties (same arrival time): queue order A, B, C Linux has a real FCFS policy: SCHED_FIFO. A SCHED_FIFO thread runs until it blocks, yields or a higher-priority thread arrives; equal priorities go in arrival order (sched(7)). C99 / POSIX: #include <sched.h> #include <stdio.h> int main(void) { printf("SCHED_FIFO priorities: %d to %d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO)); return 0; } on Linux prints: SCHED_FIFO priorities: 1 to 99A runs; B and C wait
A holds the CPU from 0 to 100 seconds. B and C need only 10 seconds each, yet they sit in the queue the whole time. Short jobs stuck behind a long one: this is called the convoy effect.
FCFS, time 0 to 100 CPU A runs 0-100 queue B waiting, C waiting convoy effect: short jobs queue up behind one heavy job, like a long line at one tillThen B, then C
B finally runs from 100 to 110, and C from 110 to 120. Turnaround is finish time minus arrival time. All three arrived at 0, so their turnarounds are 100, 110 and 120 seconds.
FCFS schedule: A arrives 0 runs 0-100 turnaround 100 - 0 = 100 B arrives 0 runs 100-110 turnaround 110 - 0 = 110 C arrives 0 runs 110-120 turnaround 120 - 0 = 120FCFS average: 110 seconds
Average turnaround: (100 + 110 + 120) / 3 = 110 seconds. B and C each ran for 10 seconds but finished after more than 100. The order of the queue, not the work itself, made them slow.
average turnaround (100 + 110 + 120) / 3 = 110 s average response (0 + 100 + 110) / 3 = 70 s (response = first run - arrival) FCFS is simple and fair in one sense: nobody jumps the queue. But one long job delays everyone behind it.Shortest job first
Same three jobs, new rule. Shortest job first (SJF) picks the ready job with the shortest run time, then the next shortest, and so on. A started job still runs to the end. The scheduler must know every run time in advance.
SJF: among the ready jobs, run the one that needs the least CPU time; run it to the end; repeat ties: queue order (B before C) B 10 s C 10 s A 100 s -> order B, C, AB, C, then A
B runs from 0 to 10, C from 10 to 20, then A from 20 to 120. A now waits 20 seconds, but B starts at once and C waits only 10. The CPU still finishes all the work at 120.
SJF schedule: B arrives 0 runs 0-10 turnaround 10 - 0 = 10 C arrives 0 runs 10-20 turnaround 20 - 0 = 20 A arrives 0 runs 20-120 turnaround 120 - 0 = 120 the CPU is busy 0-120 in both runs: same work, new orderSJF average: 50 seconds
Average turnaround: (10 + 20 + 120) / 3 = 50 seconds, less than half of FCFS's 110. When all jobs arrive together, SJF gives the lowest average turnaround of any order.
SJF average turnaround (10 + 20 + 120) / 3 = 50 s FCFS average turnaround 110 s SJF average response (0 + 10 + 20) / 3 = 10 s why: every job waits for all the jobs before it, so the shortest jobs should go first. Moving a long job earlier only adds its length to more waits.Late arrivals
Now B and C arrive at time 10. At time 0 only A is ready, so SJF starts A, and A runs to the end. B and C wait anyway: (100 + 100 + 110) / 3 = 103.33 seconds average.
SJF, B and C arrive at 10: A arrives 0 runs 0-100 turnaround 100 - 0 = 100 B arrives 10 runs 100-110 turnaround 110 - 10 = 100 C arrives 10 runs 110-120 turnaround 120 - 10 = 110 average turnaround ((100 - 0) + (110 - 10) + (120 - 10)) / 3 = 103.33 s SJF never stops a running job, so a short job that arrives late still waits behind a long one.What SJF really needs
SJF needs two things. It must know run times in advance, which a real OS rarely does. And to help late short jobs it must stop a running job, called preemption. Preemptive schedulers, such as round robin, come next.
the whole example as a Python simulation: def schedule(jobs, policy): # jobs: (name, arrival, run time) t, turnaround = 0, [] waiting = sorted(jobs, key=lambda j: j[1]) while waiting: ready = [j for j in waiting if j[1] <= t] if not ready: # nothing has arrived yet t = waiting[0][1] continue if policy == "SJF": job = min(ready, key=lambda j: j[2]) else: # FCFS: earliest arrival job = ready[0] waiting.remove(job) t += job[2] # runs to the end turnaround.append(t - job[1]) return sum(turnaround) / len(turnaround) same = [("A", 0, 100), ("B", 0, 10), ("C", 0, 10)] late = [("A", 0, 100), ("B", 10, 10), ("C", 10, 10)] print("FCFS ", schedule(same, "FCFS")) print("SJF ", schedule(same, "SJF")) print("SJF late ", round(schedule(late, "SJF"), 2)) prints: FCFS 110.0 SJF 50.0 SJF late 103.33
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Jobs A (100 s), B (10 s) and C (10 s) all arrive at time 0, in that order. Under FCFS, when does C finish?
FCFS runs A from 0 to 100, B from 100 to 110 and C from 110 to 120. C arrived at 0, so its turnaround is 120 seconds.
02What is the convoy effect?
Under FCFS, B and C need 10 seconds each but wait 100 and 110 seconds behind A, like a short shopping trip stuck behind a full trolley at a single till.
03Same three jobs, all arriving at 0, scheduled with SJF. What is the average turnaround?
SJF runs B (finishes at 10), C (at 20), then A (at 120): (10 + 20 + 120) / 3 = 50 seconds, less than half of FCFS's 110.
04A arrives at 0; B and C arrive at 10. Why does non-preemptive SJF still do badly?
SJF only chooses when the CPU is free. A starts at 0 and runs to 100, so B and C wait anyway: (100 + 100 + 110) / 3 = 103.33 seconds. Stopping A would need preemption.
05Why don't general-purpose operating systems simply use SJF?
SJF is only as good as its knowledge of run times, and a general-purpose OS usually knows very little about how long a job will take. The policies later in this module are built to work without that knowledge.
Glossary
- Scheduler
- The part of the kernel that picks which ready job (process or thread) runs on the CPU next.
- Ready queue
- The list of jobs that are ready to run and are waiting only for the CPU.
- Turnaround time
- How long a job takes from arriving to finishing: completion time minus arrival time.
- FCFS (first come first served)
- A policy that runs jobs in the order they arrived, each to the end. Also called FIFO, first in, first out.
- SJF (shortest job first)
- A policy that runs the ready job with the shortest run time first, then the next shortest, and so on.
- Convoy effect
- Short jobs piling up behind one long job, so they wait much longer than they run.
- Non-preemptive
- Once a job starts it keeps the CPU until it finishes; the scheduler only chooses when the CPU is free.
- Preemption
- Stopping a running job so that another can run, and resuming it later. It needs the timer interrupt and a context switch.
Notes and sources
- The worked example and its numbers (A 100 s, B and C 10 s; averages 110, 50 and 103.33 s) are from OSTEP chapter 7. Real CPU bursts are usually milliseconds, not seconds; the arithmetic is the same.
- FCFS and SJF here are non-preemptive: once a job starts it runs to the end. Adding preemption to SJF gives shortest time-to-completion first (STCF), which in the late-arrival case gets the average back down to 50 s (OSTEP 7.5).
- SJF gives the lowest average turnaround only under the assumptions of the example: every job arrives at the same time, uses only the CPU, and its run time is known. A general-purpose OS usually knows very little about how long a job will run.
- Linux does not use SJF for ordinary programs; its default policy is a fair-share scheduler (CFS in sched(7); EEVDF since Linux 6.6). Its real-time SCHED_FIFO policy is first come first served among threads of the same priority, without time slices. xv6, the teaching OS of MIT 6.1810, uses round robin.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 7: Scheduling: Introduction
- Berkeley CS162, Lecture 10: Scheduling 1: Concepts and Classic Policies
- MIT 6.1810 (2024), xv6: a simple, Unix-like teaching operating system, ch. 7: Scheduling
- Linux man-pages: sched(7)
- Linux man-pages: sched_get_priority_max(2)
- The Linux Kernel documentation: EEVDF Scheduler
Quiz Unlocks after the last step.