Skip to main content

Lesson 5 of 26 · 12 min read

CPU scheduling: FIFO, SJF, STCF, and Round Robin

Compare turnaround time and response time across classic scheduling policies, and test the convoy effect and preemption interactively.

Four scheduling dimensions: FIFO convoy, SJF / STCF, Round Robin, and Turnaround / Response.
Compare scheduling policies along turnaround time and response time across concrete job workloads.
Course syllabus · lesson 5 of 26
In this lesson

OSTEP Chapter 7 crux — How to develop scheduling policy. What assumptions describe the workload, which measurements distinguish good schedules, and what do the earliest policies teach us? Start with one CPU and known job lengths. Relax one assumption at a time so each change in the timeline has an identifiable cause.

The CPU can switch away from a job when its quantum ends or when it blocks. A scheduling policy decides which ready job gets the CPU. For the initial models, jobs have one CPU burst each, switching and scheduler decisions consume no modeled time, and the CPU does useful work whenever any job is ready. Treat each interval as including its start and excluding its end. A job occupying [0, 2) uses two ticks and finishes at time 2.

Timelines for Job A (5 ticks) and Job B (2 ticks) arriving at t=0 under FIFO, SJF/STCF, and Round Robin (q=1), with exact mean turnaround and mean response comparisons.
Open full-size diagram
Same two jobs at t=0 (A=5 ticks, B=2 ticks): FIFO suffers the convoy effect, SJF minimizes turnaround, and Round Robin minimizes response.

Compare scheduling timelines live

Workload: Job A (5 ticks, arrives t=0) and Job B (2 ticks, arrives t=0), ignoring context-switch overhead. Switch policies or add Job C (3 ticks, arrives t=1).

Play the CPU execution trace

Job A runs from t=0 to t=1. CPU slots: A, A, A, A, A, B, B.CPU · one tick per slotA0A1A2A3A4B5B6

Step 1 of 7

Job A runs from t=0 to t=1

Job A has 4 ticks of CPU time left.

  1. Job At=0
  2. Job At=1
  3. Job At=2
  4. Job At=3
  5. Job At=4
  6. Job Bt=5
  7. Job Bt=6

Per-job breakdown

Job A: response 0t · turnaround 5t (ran t=0..5)

Job B: response 5t · turnaround 7t (ran t=5..7)

Workload averages

Mean turnaround: 6.00 ticks

Mean response: 2.50 ticks

Tradeoff: FIFO lets A block B (convoy effect), hurting both averages.

Measure two different delays

For a job arriving at a, first dispatched at s, and completing at c, turnaround is c - a and response is s - a. Response measures time to the first opportunity to run. The time until a user sees an answer can also include computation, I/O, and further periods waiting for the CPU. Turnaround includes everything between entry and completion. It is a per-job latency, while throughput counts completions per unit time. Policies can change mean turnaround even when they finish the entire workload at the same time.

Use the same input for each comparison unless changing it deliberately. Jobs A and B arrive together at time 0. A needs five ticks and B needs two. Break arrival-order ties in name order. Keep their arrival times in the job records even when they are preempted. Overwriting the arrival timestamp with the most recent enqueue time would measure only the last wait and understate both metrics.

FIFO and the convoy effect

First In, First Out dispatches the longest-waiting arrival and in this model lets it run to completion. A runs during [0, 5) and B during [5, 7). A responds at 0 and completes at 5. B responds at 5 and completes at 7. Mean response is (0 + 5) / 2 = 2.5 ticks and mean turnaround is (5 + 7) / 2 = 6 ticks.

A FIFO ready queue needs a head and tail. Append each arrival and remove the head when the processor is free. If each job node links to the next, both operations can take constant time. In the simplified non-preemptive model the current job stays off the ready queue while running. The convoy effect follows from that policy: a long burst at the front delays all the short bursts behind it. The implementation can be fast while the workloads it produces have long waits.

SJF and the adjacent-pair argument

Shortest Job First picks the shortest ready job and lets it finish. It chooses B for [0, 2) and A for [2, 7). Mean response falls to (0 + 2) / 2 = 1 tick and mean turnaround to (2 + 7) / 2 = 4.5 ticks. The last completion still occurs at time 7.

Why is the mean turnaround optimal when all jobs arrive together? Consider neighboring jobs with durations x and y starting at time t. If x goes first, their completion sum is 2t + 2x + y. If y goes first, it is 2t + 2y + x. When x > y, putting the shorter job first reduces the sum by x - y. Neither order changes when the subsequent job can start. Repeatedly remove such inversions and you reach ascending duration order. Because arrivals are fixed, minimizing completion sum also minimizes turnaround sum. Dividing by job count gives the mean. With simultaneous arrivals and no I/O, preemption offers no further improvement: for any preemptive schedule, running jobs to completion in that schedule's completion order can finish each at least as early.

A min-heap can represent ready jobs by burst length, with a tie-break field for deterministic traces. It supports insertion and removal in logarithmic time. The model assumes those burst lengths are available to the scheduler. A priority queue cannot recover a missing or badly estimated duration.

STCF and a late arrival

Change B's arrival to time 1. SJF must choose A at time 0 because A is the only job present. At time 1, even though B is shorter, non-preemptive SJF leaves A running. A still completes at time 5, and B completes at time 7. B's turnaround is now 7 - 1 = 6 and its response is 5 - 1 = 4.

Shortest Time-to-Completion First is the preemptive version of SJF. It compares the work left rather than the initial duration. At time 1, A has four ticks left and B needs two. Run A during [0, 1), B during [1, 3), and A during [3, 7). Their turnaround values are A 7 - 0 = 7 and B 3 - 1 = 2. The mean is 4.5 ticks, down from 5.5 under non-preemptive SJF for these arrivals. Both jobs respond immediately. A individually completes later because the policy advances B ahead of it.

Before comparing the current job with a new arrival, debit the CPU already used. For example, if B arrived at time 4 instead, A would have one tick left. Comparing against A's original five ticks would preempt the wrong job. Ties can keep the running job on the CPU and avoid an unnecessary switch. Under a sustained stream of shorter jobs, a long job may wait a very long time. Mean turnaround alone does not capture that individual delay.

Round Robin and quantum cost

Round Robin returns an unfinished, still-ready job to the tail after at most one quantum, q. Reset to the original two jobs arriving at time 0 and set q = 1. The tick sequence is A, B, A, B, A, A, A. After the first two ticks, A has four left and B has one left. B gets its second tick during [3, 4) and exits. A then receives three consecutive ticks because no other work is ready. Responses are 0 and 1, averaging 0.5. Turnarounds are 7 and 4, averaging 5.5.

Here is the core of a discrete-event simulation for jobs with no I/O. admit_through inserts arrivals in order, including arrivals exactly when a slice ends. The outer event loop handles an empty queue by advancing to the next arrival and then admitting it.

Code exampleC
struct job *job = pop_front(&ready);
if (job->first_run < 0)
    job->first_run = now;
int ran = job->remaining < quantum ? job->remaining : quantum;
job->remaining -= ran;
now += ran;
admit_through(&ready, now);
if (job->remaining == 0)
    job->completion = now;
else
    push_back(&ready, job);

Set first_run once, before charging execution time. Use the smaller of the quantum and remaining work so a completed job leaves immediately. Charge the work before recording completion to put the timestamp at the end of the slice. Admit arrivals before requeueing the incumbent to implement the stated event-order convention. Reversing the two queue operations changes who runs at a boundary. A real dispatch loop must distinguish a blocked job from one that uses its slice and remains ready.

A shorter quantum bounds how long a queued job waits for each runnable job ahead of it to take a turn. If there are k jobs ahead, each getting at most q, and new arrivals join behind it, the queueing component is at most k * q, before overhead. For fully used slices with one switch of cost d per slice, the fraction of wall time spent switching is d / (q + d). At d = 1 ms, that is 1 / 11 with q = 10 ms and 1 / 2 with q = 1 ms. A lone runnable job need not incur a full process switch each quantum. Cache effects can also make actual switch cost vary.

Add blocking and overlap

Suppose A needs two ticks of CPU, three ticks of device I/O, then one final CPU tick. B needs four CPU ticks and is ready at time 0. Start A during [0, 2), then submit its I/O during [2, 5). The scheduler can immediately run B during [2, 6). A becomes ready at time 5 and, under non-preemptive dispatch between bursts, runs again during [6, 7). The CPU works seven ticks in seven units of elapsed time. Leaving it idle for all three I/O ticks would require ten units to perform the same work. A had three blocked ticks and one ready tick before its final burst.

A scheduler can treat each CPU burst as a new scheduling opportunity and apply a policy on wakeup. The completion interrupt tells it a task can run again. It does not disclose how long the next CPU burst will last. Predicting bursts from history adds a new assumption. Policies such as MLFQ use observed CPU use instead of requiring exact predictions.

Worked trace

Bring back A with five ticks and B with two ticks, both arriving at 0. Add C with three ticks arriving at 1. Use the same arrival-before-requeue convention for Round Robin.

To check the trace in the scheduling lab above, enable Job C and step through one policy at a time. Record the first slot occupied by each job and the boundary just after its last slot. The first slot is its start time, and the boundary is its completion time. Counting the last occupied slot itself as the finish would make every turnaround one tick too small.

  1. FIFO. Run A [0, 5), B [5, 7), then C [7, 10). In A, B, C order, turnaround is 5, 7, 9 and response is 0, 5, 6. The means are 7 and 11 / 3.
  2. SJF and STCF. B runs first. At time 1, B's one remaining tick is shorter than C's three, so STCF keeps B. Both policies give B [0, 2), C [2, 5), and A [5, 10). Turnaround is 10, 2, 4 and response is 5, 0, 1. The means are 16 / 3 and 2.
  3. Round Robin, one tick per turn. After A's first tick, the queue is B, C, A. The full sequence is A, B, C, A, B, C, A, C, A, A. Finish times are 10, 5, 8 and first-run times are 0, 1, 2. Subtract C's arrival of 1 for both metrics. The means are 22 / 3 turnaround and 2 / 3 response.

Change one input

Move B's arrival to time 1 while leaving C's arrival at 1. Under STCF, A starts at 0, B preempts it at 1, C starts at 3, and A resumes at 6. Compute the means from the resulting finish times 10, 3, 6 in A, B, C order. They are 17 / 3 turnaround and 2 / 3 response. Non-preemptive SJF keeps A running to time 5, so the change finally separates the two shortest-job policies.

Next, continue to MLFQ, proportional share, and multicore scheduling.

Before you move on

Try it in your workspace

On paper, draw timelines for Job A (5 ticks) and Job B (2 ticks) arriving at t=0 under FIFO (A first) and Round Robin (q=1, A first). Compute mean turnaround and mean response, then verify in the interactive lab above.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.