Operating Systems Module 3: CPU scheduling Lesson 3.2 12 min

FCFS and shortest job first

First come, first served and its convoy effect; shortest job first, which is optimal for average waiting time but needs to know the future; its preemptive version SRTF; and starvation.

A print shop has one printer. A student sends a thesis of several hundred pages, and a moment later a handful of people each send a single page. Should they all wait for the thesis? If the shop serves in arrival order, they will. If it prints the short jobs first, everyone else leaves happy and the thesis is barely later.

That is the whole argument of this lesson, made precise with two schedulers you can already guess from their names.

First come, first served

FCFS (first come, first served) runs processes in the order they became ready, and lets each one run until it finishes or blocks. It is the simplest possible scheduler: a queue, nothing more. It is fair in one sense (nobody jumps the queue) and needs no information about the jobs.

Its weakness is what happens when a long job is at the front. Every short job behind it waits for the whole of it, like cars stuck behind a slow lorry on a narrow road. This is the convoy effect: one long CPU burst holds up a crowd of short ones, and the average wait balloons.

Shortest job first

SJF (shortest job first) looks at every ready process and runs the one with the shortest next CPU burst. Once a job starts, it runs until it finishes or blocks. When two jobs are equally short, the one that arrived first goes first, and if they arrived together, the one listed first.

Here are four jobs that all arrive at time 0: one long, three short. The top pane is SJF. The bottom pane is FCFS on exactly the same input, kept at the same moment as you step through. Watch the order the bars are drawn in.

Shortest job first, compared with FCFS

  1. P1, P2, P3 and P4 arrive P1, P2, P3 and P4 arrive and join the ready queue. Ready queue: P4 → P3 → P2 → P1.
    Time
    0 ms
    Running
    idle
    Ready queue
    P4 → P3 → P2 → P1
    P1
    12 of 12 ms left
    P2
    5 of 5 ms left
    P3
    2 of 2 ms left
    P4
    1 of 1 ms left
  2. P4 runs The scheduler picks P4, because its next CPU burst, 1 ms, is the shortest.
    Time
    0 ms
    Running
    P4
    Ready queue
    P3 → P2 → P1
    P1
    12 of 12 ms left
    P2
    5 of 5 ms left
    P3
    2 of 2 ms left
    P4
    1 of 1 ms left
  3. P4 finishes, P3 runs P4 finishes at 1 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 1 ms. The scheduler picks P3, because its next CPU burst, 2 ms, is the shortest.
    Time
    1 ms
    Running
    P3
    Ready queue
    P2 → P1
    P1
    12 of 12 ms left
    P2
    5 of 5 ms left
    P3
    2 of 2 ms left
    P4
    done at 1: waited 0, turnaround 1
  4. P3 finishes, P2 runs P3 finishes at 3 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 3 ms. The scheduler picks P2, because its next CPU burst, 5 ms, is the shortest.
    Time
    3 ms
    Running
    P2
    Ready queue
    P1
    P1
    12 of 12 ms left
    P2
    5 of 5 ms left
    P3
    done at 3: waited 1, turnaround 3
    P4
    done at 1: waited 0, turnaround 1
  5. P2 finishes, P1 runs P2 finishes at 8 ms. It waited 3 ms in all, and its turnaround (arrival to finish) was 8 ms. The scheduler picks P1, because it is the only process ready.
    Time
    8 ms
    Running
    P1
    Ready queue
    empty
    P1
    12 of 12 ms left
    P2
    done at 8: waited 3, turnaround 8
    P3
    done at 3: waited 1, turnaround 3
    P4
    done at 1: waited 0, turnaround 1
  6. P1 finishes P1 finishes at 20 ms. It waited 8 ms in all, and its turnaround (arrival to finish) was 20 ms. Every process is done at 20 ms. The average wait is 3 ms.
    Time
    20 ms
    Running
    idle
    Ready queue
    empty
    P1
    done at 20: waited 8, turnaround 20
    P2
    done at 8: waited 3, turnaround 8
    P3
    done at 3: waited 1, turnaround 3
    P4
    done at 1: waited 0, turnaround 1
    Average wait
    3 ms
    Average turnaround
    8 ms
    Average response
    3 ms

The summary row says it all. FCFS put the 12 ms job first, and the three short jobs waited 12, 17 and 19 ms behind it: a convoy. SJF ran them shortest first, and the average wait fell from 12 ms to 3 ms. The long job paid for it: P1 waited 8 ms instead of 0.

Why SJF is the best for average waiting time

Every job that runs delays every job after it by its own length. A short job at the front delays the others a little; a long job at the front delays them a lot. So the average wait is smallest when the shortest job goes first, then the next shortest, and so on. Swap any two neighbours that are in the wrong order and the average gets worse. That is why, for jobs that are all ready at once, SJF gives the lowest possible average waiting time. No scheduler can beat it on that measure.

There is a catch, and it is a big one: SJF must know how long each job’s next CPU burst will be. The kernel cannot see the future. A process does not announce “I will compute for 12 ms, then read a file”.

SRTF: shortest remaining time first

Plain SJF only chooses when the CPU becomes free. If a short job arrives while a long one is running, it waits. SRTF (shortest remaining time first) is the preemptive version: when a new job arrives, the scheduler compares its burst with what the running job still needs, and if the newcomer is shorter, it takes the CPU straight away.

Big is a 9 ms job that starts at time 0. Tiny, a 2 ms job, arrives at 3 ms. The bottom pane runs plain SJF on the same two jobs.

SRTF lets a short job cut in

  1. Big arrives Big arrives and joins the ready queue. Ready queue: Big.
    Time
    0 ms
    Running
    idle
    Ready queue
    Big
    Big
    9 of 9 ms left
    Tiny
    arrives at 3
  2. Big runs The scheduler picks Big, because it is the only process ready.
    Time
    0 ms
    Running
    Big
    Ready queue
    empty
    Big
    9 of 9 ms left
    Tiny
    arrives at 3
  3. Tiny arrives Tiny arrives and joins the ready queue. Ready queue: Tiny.
    Time
    3 ms
    Running
    Big
    Ready queue
    Tiny
    Big
    6 of 9 ms left
    Tiny
    2 of 2 ms left
  4. Tiny preempts Big Tiny needs 2 ms, less than the 6 ms Big still needs, so Big is preempted and goes back to the ready queue. The scheduler picks Tiny, because its next CPU burst, 2 ms, is the shortest.
    Time
    3 ms
    Running
    Tiny
    Ready queue
    Big
    Big
    6 of 9 ms left
    Tiny
    2 of 2 ms left
  5. Tiny finishes, Big runs Tiny finishes at 5 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 2 ms. The scheduler picks Big, because it is the only process ready.
    Time
    5 ms
    Running
    Big
    Ready queue
    empty
    Big
    6 of 9 ms left
    Tiny
    done at 5: waited 0, turnaround 2
  6. Big finishes Big finishes at 11 ms. It waited 2 ms in all, and its turnaround (arrival to finish) was 11 ms. Every process is done at 11 ms. The average wait is 1 ms.
    Time
    11 ms
    Running
    idle
    Ready queue
    empty
    Big
    done at 11: waited 2, turnaround 11
    Tiny
    done at 5: waited 0, turnaround 2
    Average wait
    1 ms
    Average turnaround
    6.5 ms
    Average response
    0 ms

With SRTF, Tiny did not wait at all and Big waited only 2 ms. Under plain SJF, Tiny had to sit through the rest of Big and waited 6 ms. Preemption cost one extra switch and bought a much better average.

Starvation

Both SJF and SRTF have a dark side. Suppose short jobs keep arriving, one after another. Each one is shorter than the long job, so each one goes first. The long job is always ready and never runs. This is starvation: a process that waits indefinitely, not because something is broken, but because the rule always favours someone else.

Press Try it on the SRTF sim and add a few more short jobs that arrive while Big is waiting, for example T2 arrive 5 burst 2 and T3 arrive 7 burst 2. Watch Big’s wait grow with each one.

In the real world

Real kernels cannot know the next burst, so the classic idea is to predict it from the past. After each burst, the scheduler updates its guess for that process: the new guess is a weighted average of the burst that just happened and the old guess. Recent behaviour counts most, but one odd burst does not throw the guess off completely. This is called exponential averaging.

Modern general-purpose schedulers do not run SJF directly, but the idea survives. A process that runs only briefly before blocking (a text editor, a shell) tends to get the CPU quickly when it wakes up, because it has used little CPU time. You will see how Linux turns “used little so far” into a rule in Lesson 3.5.

Recap

  • FCFS runs jobs in arrival order. It is simple, but one long job causes a convoy effect.
  • SJF runs the shortest next CPU burst first. It gives the lowest possible average waiting time.
  • SJF needs to know the future, so real systems can only estimate bursts.
  • SRTF is preemptive SJF: a newly arrived shorter job takes the CPU at once.
  • Both can cause starvation: a long job may wait for ever if short ones keep coming.

Quick check

Quick check

  1. Why is shortest job first hard to use in a real operating system?
  2. What is starvation in CPU scheduling?
  3. What is the convoy effect?
  4. A short job arrives while a long job is running. What does SRTF do that SJF does not?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion