E ExamMaster

CS Core & Software Engineering · Operating Systems

CPU scheduling

Turnaround, waiting and response, then FCFS, SJF, SRTF, Round Robin and priority on one three-job walk.

Maya's laptop has one CPU. Three programs want it: Print (burst 4), Spell (burst 3) and Save (burst 5). They become Ready at time 0 unless a later card says otherwise. Every Gantt chart in this lesson is a different order of those jobs. Metrics first — turnaround, waiting, response — then FCFS, SJF, SRTF, Round Robin, priority with aging, and the preemptive cut.

  • CS Core & Software Engineering
  • Medium level
  • 7 concepts
  • 5 practice questions

1Turnaround, waiting and response

Maya's laptop has one CPU and three programs that want it. Call them Print (burst 4), Spell (burst 3) and Save (burst 5). Burst time is how long a job needs the CPU in total. Arrival time is when it became Ready. Completion time is when it finished. Those three clocks give three scores.

Turnaround time is completion minus arrival — how long the job lived in the system. Waiting time is turnaround minus burst — how long it sat in Ready. Response time is the first moment on the CPU minus arrival — how long until it first ran. Waiting and response are not the same: a job can run, be preempted, and wait again.

One walk already on this card: a job arrives at 2, first runs at 5, completes at 12, burst 7. TAT = 12 − 2 = 10. WT = 10 − 7 = 3. Response = 5 − 2 = 3.

Figure. The wide bar is lifetime in the system (AT to CT). Response is only the left stub until first dispatch; waiting is TAT minus the burst, not that stub alone.

How it works

  1. Read completionOff the Gantt chart's right edge for that process — that is CT.
  2. TurnaroundTAT = CT − AT. This is always non-negative and at least the burst.
  3. Waiting and responseWT = TAT − BT. Response = first-on-CPU − AT. Average WT is the sum of waiting times over the number of processes.

One process, three metrics

A process arrives at time 2, first runs at time 5, completes at time 12, and has burst 7. Find TAT, WT and response time.

  • TAT = CT − AT = 12 − 210
  • WT = TAT − BT = 10 − 73
  • Response = first-on-CPU − AT = 5 − 23

Pro tip. Always compute CT → TAT → WT in that order. Skipping CT and guessing waiting from the chart is where most GATE arithmetic slips.

Arrival 0, burst 5, completion 12, first dispatch at 4. Waiting time is
  1. 7
  2. 8
  3. 4

TAT = 12 − 0 = 12; WT = 12 − 5 = 7. Response is 4, which is a different metric. Completion minus start (12 − 4 = 8) is not waiting time once you have left and returned to the CPU.

2FCFS and the convoy

First-Come-First-Served runs Ready jobs in arrival order and does not steal the CPU for another Ready job. Print, Spell and Save all arrive at time 0 in that order, bursts 4, 3, 5. FCFS runs Print 0→4, Spell 4→7, Save 7→12.

Waiting times: Print 0, Spell 4, Save 4+3 = 7. Average 11/3 ≈ 3.67. The convoy effect is the same picture with a long job at the head: everyone behind sits idle. FCFS is simple and unfair to short jobs stuck behind a long one.

Figure. FCFS Gantt for bursts 4, 3, 5 in arrival order. Block widths are proportional to burst (scale 0.07 per time unit from x = 0.08). Waiting times are the left edges: 0, 4, 7.

How it works

  1. Queue by arrivalThe ready queue is a FIFO. The process that arrived earliest runs next.
  2. Run to completionNo timer steals the CPU for another ready process; FCFS yields only on finish or block.
  3. Read waitingWith equal arrivals, each process waits for the sum of all bursts ahead of it in the order.

FCFS average waiting time

Processes arrive at time 0 with bursts P1=4, P2=3, P3=5 in order P1, P2, P3 under FCFS. Find average waiting time.

  • WT(P1) = 0; WT(P2) = 4; WT(P3) = 4 + 30, 4, 7
  • Sum of waiting times11
  • Average WT = 11 / 3≈ 3.67

Pro tip. In FCFS with simultaneous arrivals, each process waits exactly for the sum of all previous burst times — no Gantt arithmetic beyond that chain.

Coding lab. FCFS waiting times for Print, Spell, Save runs in the app, with checks on your output.

Three jobs arrive together with bursts 20, 2, 2 and run FCFS in that order. The convoy effect mainly hurts
  1. The two short jobs waiting behind the long one
  2. The long job, which is forced to finish last
  3. Only response time; average waiting time is unchanged

The short jobs sit idle while the length-20 job runs. The long job itself is not delayed by the convoy; average waiting time rises because those short waits pile up.

3SJF picks the shortest burst

Shortest-Job-First always picks the Ready job with the smallest burst. Among non-preemptive policies it minimises average waiting time. Same three jobs, all arrived at 0: Spell (3), then Print (4), then Save (5).

Waiting: Spell 0, Print 3, Save 3+4 = 7. Average 10/3 ≈ 3.33, below FCFS's 3.67. The cost: bursts must be known or guessed, and a stream of short jobs can starve Save forever.

Figure. SJF Gantt for the same three bursts: shortest first. Widths still use 0.07 per time unit. Waiting times 0, 3, 7 — total 10 versus FCFS's 11.

How it works

  1. Sort by burstWhen the CPU is free, choose the ready process with the shortest declared burst.
  2. Run non-preemptivelyThat process keeps the CPU until it finishes or blocks; newcomers wait their turn by burst length, not arrival.
  3. Trade-offAverage WT drops versus FCFS on the same set, but long jobs can wait indefinitely if short jobs keep arriving.

SJF beats FCFS on the same bursts

Same bursts P1=4, P2=3, P3=5, all arriving at 0. Under non-preemptive SJF, find average waiting time.

  • SJF order by burstP2 (3), P1 (4), P3 (5)
  • WT: P2 = 0, P1 = 3, P3 = 3 + 40, 3, 7
  • Average WT = 10 / 3≈ 3.33 (below FCFS 3.67)

Pro tip. Shortest-first always beats other non-preemptive orders for average waiting time when arrival times are equal — that is the optimality claim GATE leans on.

A GATE question asks which non-preemptive policy gives the minimum possible average waiting time. The answer is
  1. SJF
  2. FCFS
  3. Priority without aging

SJF is optimal among non-preemptive policies for average waiting time. FCFS ignores burst length; priority optimises a different criterion and can starve.

4SRTF re-evaluates on every arrival

Shortest-Remaining-Time-First is preemptive SJF. Whenever a new job arrives, compare its burst to the running job's remaining time and switch if the newcomer is shorter. Re-check at every arrival, not only when a job ends.

A later morning on the same laptop: Print arrives at 0 with burst 7; Spell arrives at 2 with burst 4. Print runs 0→2 (5 left). Spell's 4 is shorter than 5, so Print is preempted. Spell runs 2→6. Print finishes 6→11. Waiting: Print 11−7 = 4, Spell 0. Average 2.

Figure. SRTF Gantt: P1 runs until t = 2, yields to shorter P2 (burst 4 vs remaining 5), then finishes after P2. Segment widths use 0.84/11 per time unit from x = 0.08. Not to be read as FCFS — P1 appears twice.

How it works

  1. Run shortest remainingAmong ready processes, the CPU holds the one with the least time left.
  2. Arrival interruptOn each new arrival, compare the new burst to the current remaining time; preempt if the newcomer wins.
  3. Resume laterThe preempted process returns to Ready with its reduced remaining time and competes again.

Arrival steals the CPU

P1 arrives at 0 with burst 7; P2 arrives at 2 with burst 4. Under SRTF, find each waiting time and the average.

  • P1 runs 0→2 (remaining 5); P2 arrives with burst 4 < 5preempt P1
  • P2 runs 2→6 (done); P1 runs 6→11CT: P2=6, P1=11
  • WT: P1 = 11 − 7, P2 = 6 − 2 − 4; average4, 0; avg = 2

Pro tip. If you only reshuffle when a job finishes, you drew non-preemptive SJF. SRTF's decision point is every arrival tick.

Under SRTF, a new process arrives while another is running. The scheduler
  1. Compares the new burst to the running process's remaining time and may preempt
  2. Ignores the arrival until the current process finishes
  3. Always lets the new process run immediately regardless of burst

SRTF preempts only when the newcomer is strictly shorter than what remains. Ignoring arrivals until finish is non-preemptive SJF; always switching is not shortest-remaining.

5Round Robin and the quantum

Round Robin gives every Ready job a fixed time quantum in a circle. Fair, and the usual choice when a human is waiting on a response. Too small a quantum explodes context-switch overhead. If the quantum is at least the longest burst, nobody is ever cut and Round Robin is FCFS.

Print burst 4 and Spell burst 2, both arrive at 0, quantum 2. Slice 1: Print 0→2 (2 left). Slice 2: Spell 2→4 (done). Slice 3: Print 4→6 (done). Print's completion time is 6.

Figure. RR Gantt with quantum 2: equal-width slices (0.28 = 2 × 0.14). P1 appears twice because its leftover returns after P2 finishes.

How it works

  1. Dispatch for qThe head of the ready queue runs for at most one quantum.
  2. Expire or finishIf it still has remaining time when q expires, it is preempted and rejoins the tail; if it finishes earlier, the next process starts immediately.
  3. Tune qSmall q ≈ processor sharing with heavy switching; large q ≈ FCFS.

RR completion with quantum 2

P1 and P2 arrive at 0 with bursts 4 and 2; quantum = 2. Find P1's completion time.

  • Slice 1: P1 runs 0→2 (remaining 2)P1 → tail
  • Slice 2: P2 runs 2→4P2 done, CT = 4
  • Slice 3: P1 runs 4→6P1 CT = 6

Pro tip. Trace remaining bursts after every slice. The process that just expired goes to the tail before the next dispatch — missing that reorder is the usual RR slip.

Under Round Robin with quantum 3, P1 and P2 arrive at 0 with bursts 5 and 3. P2 finishes at
  1. Time 6
  2. Time 3
  3. Time 8

P1 runs 0→3 (remaining 2), then P2 runs 3→6 and completes its full burst of 3. P2 does not wait for P1's leftover.

6Priority and aging

Priority scheduling always runs the Ready job with the best priority. Convention varies — sometimes a smaller number wins. Static priorities starve: if Print keeps arriving with a better priority, Save never runs.

Aging fixes starvation by gradually raising the priority of a job that has waited a long time, so a neglected Save eventually outranks the next Print. Aging is not a different scheduler; it is a rule added to priority.

Figure. Static priority can starve low jobs; aging bumps waiters so they eventually run.

How it works

  1. Pick by priorityAmong ready processes, dispatch the highest-priority one (preemptive variants re-check on every arrival).
  2. Starvation riskA steady stream of high-priority arrivals can leave a low-priority process ready forever.
  3. Age the waitersPeriodically bump waiting processes' priorities so the oldest waiter eventually runs.
A low-priority process has been ready for a long time while high-priority jobs keep arriving. Aging will
  1. Gradually raise that process's priority until it can run
  2. Lower all high-priority bursts so FCFS takes over
  3. Convert the scheduler permanently to Round Robin

Aging only moves waiting processes up the priority scale. It does not rewrite bursts or replace the policy with RR.

7Preemptive versus non-preemptive

Preemptive scheduling may seize a still-runnable job because a better candidate appeared. SRTF, Round Robin and preemptive priority all do this. Non-preemptive scheduling lets the running job keep the CPU until it finishes or blocks. FCFS and classic SJF are non-preemptive.

A block for I/O is not that cut. maya going Running → Waiting because she read the disk is voluntary. Preemption is the timer or a newly arrived shorter job taking a process that could still have run.

Figure. Preemption lets the scheduler reclaim the CPU on a tick or higher-priority arrival; non-preemptive waits for yield.

How it works

  1. Non-preemptiveFCFS and SJF: decide only when the CPU becomes free (finish or block).
  2. PreemptiveSRTF, RR, preemptive priority: decide again on arrivals or quantum expiry while a process is still runnable.
  3. CostPreemption buys responsiveness and can cut average wait (SRTF); every extra switch is pure overhead.
Policy preemption cut
PolicyPreemptive?Classic failure mode
FCFSNoConvoy effect
SJFNoStarvation of long jobs; needs burst estimates
SRTFYesStarvation of long jobs; more context switches
Round RobinYes (quantum)Tiny quantum → switch overhead; huge quantum → FCFS
PriorityEitherStarvation unless aging
A running process is still CPU-bound when a shorter job arrives. Which policy may switch immediately?
  1. SRTF
  2. Non-preemptive SJF
  3. FCFS

SRTF re-evaluates on arrival and may preempt. Non-preemptive SJF and FCFS wait until the current process finishes or blocks.

Notes

  • FCFS schedules in arrival order (non-preemptive) and suffers the convoy effect when a long job blocks short ones.
  • SJF/SRTF picks the shortest (remaining) burst; SJF is provably optimal for minimizing average waiting time but can starve long jobs.
  • Round Robin gives each process a time quantum in a circular queue, ensuring fairness and good response time; too small a quantum raises context-switch overhead.
  • Priority scheduling runs the highest-priority process first; aging gradually raises the priority of waiting processes to prevent starvation.
  • Preemptive scheduling can interrupt a running process (SRTF, RR, preemptive priority); non-preemptive runs to completion or block (FCFS, SJF).

Formulas

  • Turnaround Time (TAT) = Completion Time - Arrival Time.
  • Waiting Time (WT) = Turnaround Time - Burst Time.
  • Response Time = First time on CPU - Arrival Time.
  • Average WT = (sum of all waiting times) / (number of processes).
  • CPU utilization = busy time / total time; throughput = processes completed per unit time.

Exam traps & shortcuts

  • SJF gives the minimum possible average waiting time — if a question asks for the optimum, it's SJF.
  • For Round Robin, if quantum >= largest burst it behaves exactly like FCFS.
  • Compute in order: Completion Time -> TAT (= CT-AT) -> WT (= TAT-BT); never skip the completion time.

Reference tables

Read CT off the Gantt chart, then apply these in order.

Metric formulas
MetricFormula
Turnaround time (TAT)CT − AT
Waiting time (WT)TAT − BT
Response timeFirst-on-CPU − AT
Average waiting time(sum of WT) / n
CPU utilisationbusy time / total time
Throughputprocesses completed per unit time

Match the policy to the failure mode before you pick an answer.

Algorithm comparison
PolicySelection ruleExam peg
FCFSArrival orderConvoy when a long job leads
SJFShortest burstMinimum average WT (non-preemptive)
SRTFShortest remainingRe-check on every arrival
Round RobinCircular quantumq ≥ max burst ⇒ behaves like FCFS
Priority + agingHighest priority; age waitersAging is the starvation fix

Recap

Print 4, Spell 3, Save 5. FCFS average wait 11/3. SJF 10/3.

Metrics
TAT = CT − AT. WT = TAT − BT. Response = first-on-CPU − AT.
FCFS
Arrival order. Convoy: long job at the head. Waits 0, 4, 7.
SJF
Shortest burst first. Minimum average wait among non-preemptive policies.
SRTF
Re-evaluate remaining time on every arrival.
RR
Quantum in a circle. Huge quantum → FCFS.
Priority
Best priority runs. Aging promotes waiters so they do not starve.
Cut
Preemption seizes a still-runnable job. An I/O block is not that.

Practise CPU scheduling

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • 5 exam-style questions on this topic, with explanations
  • A 6-question practice set that ends the chapter
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.