Reading position, not mastery.
Reading settings
Line spacing
Text width
Saved only in this browser. Theme and colors are in Appearance.
Explore, understand, practice

CPU scheduling from scratch: FCFS, SJF and timelines

Start from foundations · Course contents · فارسی

Previous lesson: Operating systems from zero: programs, processes and CPU turns · Next lesson: Preemptive scheduling: SRTF and Round Robin turns

The previous lesson distinguished Ready, able to run, from actually holding the CPU. When several processes are ready, which should run next? Choosing the first in line is simple, but a short job behind a long one still waits a long time. This takes us from “how do we switch processes?” to “which process should we select?”

Objective: construct timelines for two simple policies from arrival and execution data, then compute waiting, response and turnaround times. The only essential prerequisites are Ready versus Running from the previous lesson and basic subtraction. This begins chapter OS-02; chapter one still has topics such as threads and interprocess communication and is not complete. Round Robin, preemptive scheduling and real-time scheduling are not taught here.

1. Mechanism versus policy

A scheduler chooses the next ready process according to a policy. A context switch transfers the execution state needed to suspend one process and resume another. Selecting “who?” differs from implementing “how do we switch?”. Real systems may schedule threads; our simplified processes each have one execution stream.

For hand calculations we assume one CPU, one CPU execution interval per process, no I/O or intermediate blocking, known execution requirements and zero switching overhead. Times use illustrative units. Arrival means the process becomes ready at that moment. We do not deliberately idle the CPU when the ready queue is nonempty. Changing these assumptions can change the answer. These limitations follow the modeling approach in the OSTEP scheduling introduction; the numerical examples here are original.

2. Four times to keep separate

Symbol Meaning
A — Arrival Instant the process enters the ready queue
B — Burst Required CPU duration, not the completion instant
S — Start First instant the process receives CPU time
C — Completion Instant the process finishes

Three metrics follow:

Turnaround = C - A
Response   = S - A
Waiting    = Turnaround - B   (only under this lesson's no-I/O model)

Turnaround time is elapsed time from arrival to completion: how long until the work is finished. Response time here means arrival to first execution; it need not equal the time until a real application displays its first output. Waiting time is total time spent ready but without the CPU.

Why does Waiting=C-A-B hold in our model? Arrival-to-completion time consists only of ready waiting and CPU execution. Subtract execution and waiting remains. With I/O, turnaround also contains blocked time, so that subtraction alone does not isolate ready-queue waiting. With interrupted execution, waiting may have several pieces and need not equal initial response time.

3. FCFS: serve in arrival order

First Come, First Served (FCFS) preserves arrival order. Our version is non-preemptive: a newly arriving job does not displace a running process, which completes its CPU interval. For simultaneous arrivals we break ties by table order.

Process A B
P1 0 6
P2 0 2
P3 0 1

All three are ready at zero. The tie rule selects P1, which runs from 0 to 6. P2 has waited six units and runs from 6 to 8. P3 then runs from 8 to 9.

0       6   8  9
|  P1   |P2 |P3|
Process S C Waiting Turnaround Response
P1 0 6 0 6 0
P2 6 8 6 8 6
P3 8 9 8 9 8

Mean waiting time is the sum of waiting times divided by the number of processes: (0+6+8)/3=14/3, about 4.67. Mean turnaround is (6+8+9)/3=23/3, about 7.67. Response equals waiting here because each process runs uninterrupted from its first start to completion.

The example exposes the effect of queuing behind a long job. P3's short duration does not advance its position in FCFS. This does not establish that FCFS is always bad: simplicity and preserving queue order are benefits, and the appropriate policy depends on the objective.

4. SJF: choose the shortest ready job

Shortest Job First (SJF) selects the shortest CPU interval among jobs ready at a selection point. Here SJF is also non-preemptive: it does not reconsider the selection during an execution interval. Equal bursts are ordered by earlier arrival, then table order.

For the same table, P3 is shortest at zero and runs from 0 to 1. P2 follows from 1 to 3, then P1 from 3 to 9. Waiting times for P3, P2 and P1 are 0, 1 and 3; mean waiting is 4/3, about 1.33. Their turnarounds are 1, 3 and 9, giving a mean of 13/3, about 4.33. Total CPU execution is still nine under both policies. Reordering improves the mean; the CPU did not become faster.

Timelines for the same jobs: FCFS runs P1, P2, P3 with boundaries 0, 6, 8, 9; SJF runs P3, P2, P1 with boundaries 0, 1, 3, 9.

The diagram shows order and proportional interval lengths. The tables and prose provide the same essential information without requiring the image.

Why put shorter work first? Consider two simultaneously ready jobs of lengths x>y, with the long job first. The short job waits x behind it. Swap them: the long job waits only y behind the short one. The completion of the pair, and the start of jobs after the pair, remain unchanged. Their total waiting decreases by x-y. For independent, simultaneously available jobs on one CPU with known lengths and zero switching cost, this explains why SJF minimizes mean waiting time. Do not generalize that conclusion to every arrival pattern, other metrics or unknown durations.

Real systems usually do not know future execution durations exactly and must estimate them. If short jobs keep arriving, a long ready job may repeatedly be postponed: a risk of starvation, or indefinite postponement. Neither issue can be established by a single three-job example; remember the model's limits.

5. A major trap: do not select an unarrived job

Use new data: P1 has A=0,B=5, P2 has A=1,B=1, and P3 has A=2,B=2. Only P1 is ready at zero. Non-preemptive SJF runs it from 0 to 5. We cannot run P2 at zero or interrupt P1 when P2 arrives at 1. At 5 both P2 and P3 are ready: P2 runs from 5 to 6, then P3 from 6 to 8.

For P2, S=5,C=6: response is 5-1=4, turnaround is 6-1=5, and waiting is 5-1=4. Forgetting to subtract arrival gives a wrong answer. A policy that can interrupt the current process for a newly available job with less remaining work is Shortest Remaining Time First (SRTF); do not silently substitute it for this lesson's SJF.

If the first arrival is at 3, the interval from 0 to 3 is idle. Do not erase it or reset the first arrival to zero unless you deliberately shift every time value consistently.

6. Independent practice and exam tips

All questions are original. Draw a timeline before computing metrics; memorizing formulas alone is insufficient.

1. A and B arrive at zero with bursts 4 and 1. Break simultaneous-arrival ties A before B. What are the FCFS and SJF mean waiting times?

Answer 1

FCFS: A runs 0–4 and B runs 4–5; waits are 0 and 4, mean 2. SJF: B runs 0–1 and A runs 1–5; waits are 0 and 1, mean 0.5. Both finish all work at 5.

2. In section 5's staggered-arrival example, compute P3's waiting and turnaround.

Answer 2

P3 arrives at 2, starts at 6 and finishes at 8. Waiting and response are 6−2=4; turnaround is 8−2=6. Equivalently, turnaround 6 minus burst 2 gives waiting 4.

3. Original multiple choice: Under non-preemptive SJF, A starts at zero with burst 8; B arrives at 1 with burst 1. With no other work, when does B start? A) 0; B) 1; C) 8; D) 9.

Test answer

C, at time 8. B's arrival does not interrupt A. Nine is B's completion time, not its start.

4. A process arrives at 2, first runs at 5, and finishes at 12. It uses four CPU units and two I/O units. Compute response, turnaround and total ready waiting.

Answer 4

Response is 3 and turnaround is 10. If all elapsed time consists only of CPU execution, ready waiting and this I/O, waiting is 10−4−2=4. Subtracting only CPU time gives 6 and incorrectly includes two blocked I/O units. This question intentionally relaxes the no-I/O assumption.

5. Nothing is ready before time 3. A arrives with A=3,B=2; B arrives with A=4,B=1. Draw FCFS and find B's waiting time.

Answer 5

Idle 0–3; A runs 3–5; B runs 5–6. B waits 5−4=1. Idle time before B arrives does not count as B's waiting.

7. Retrieve and decide whether to continue

Without looking, define Arrival, Burst, Response and Turnaround in one sentence each. Use new data to explain why SJF must first ask who is ready. If the timeline is wrong, list the ready queue at every selection point. If metrics are wrong, mark arrival, first start and completion on an axis. Independently constructing a timeline and deriving its metrics is a practical continuation criterion. Try nonzero arrivals again after a gap.

References and authorship

Source checked on 2026-11-23: Arpaci-Dusseau and Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 7: Scheduling Introduction, for policy names, metrics and model assumptions. Step-by-step explanations, data, diagrams and exercises were authored for this lesson. No official exam questions or exam-frequency analysis are presented.

Prerequisite: programs and processes · Course contents · Start from foundations

Previous lesson: Operating systems from zero: programs, processes and CPU turns · Next lesson: Preemptive scheduling: SRTF and Round Robin turns