Quiz 2
Registry Synced

CPU Scheduling — FCFS, SJF, SRTF

1249 words
6 min read

Reading compass

Now · 🎯 Learning Objectives

CPU Scheduling — FCFS, SJF, SRTF

🎯 Learning Objectives

  • Calculate turnaround time, waiting time, and response time for any scheduling algorithm
  • Trace FCFS, SJF (preemptive and non-preemptive) with Gantt charts
  • Identify the convoy effect and its impact on average waiting time
  • Explain why SJF is optimal in terms of average waiting time

1. Scheduling Fundamentals

1.1 Intuition

CPU scheduling decides which process gets the CPU next. The OS scheduler (short-term scheduler) runs whenever the CPU becomes idle — typically every few milliseconds. Good scheduling makes the system feel responsive (low response time) while maximizing throughput.

1.2 Scheduling Criteria

CriterionFormulaGoal
CPU Utilizationbusy_time / total_timeMaximize (keep CPU busy)
Throughputprocesses_completed / timeMaximize
Turnaround Time (TAT)completion_time - arrival_timeMinimize
Waiting Time (WT)turnaround_time - burst_timeMinimize
Response Timefirst_response_time - arrival_timeMinimize (for interactivity)
(Diagram)

1.3 Scheduling Decision Points

  • Process switches from Running → Waiting (I/O request)
  • Process switches from Running → Ready (interrupt/timer)
  • Process switches from Waiting → Ready (I/O complete)
  • Process terminates

2. First-Come, First-Served (FCFS)

2.1 Intuition

The simplest algorithm: the first process to arrive gets the CPU first (like a queue at a ticket counter). Non-preemptive — once a process gets the CPU, it keeps it until it terminates or does I/O.

2.2 Worked Example 1

Processes:
ProcessArrival TimeBurst Time
P1024
P203
P303
Gantt Chart:
pseudo
|   P1 (24)  |  P2 (3)  |  P3 (3)  |
0            24         27         30
Calculations:
ProcessATBTStartEndTAT (End-AT)WT (TAT-BT)
P1024024240
P20324272724
P30327303027
Average TAT = (24 + 27 + 30) / 3 = 27 Average WT = (0 + 24 + 27) / 3 = 17

2.3 Worked Example 2: Convoy Effect

Processes:
ProcessArrival TimeBurst Time
P1010
P201
P301
Gantt Chart:
pseudo
|   P1 (10)  |  P2 (1)  |  P3 (1)  |
0            10         11         12
Average WT = (0 + 10 + 11) / 3 = 7 The convoy effect occurs when short processes wait behind a long one. If P2 and P3 (short) arrived right after P1 started, they still wait 10 units.

2.4 Worked Example 3: Non-zero Arrival Times

ProcessArrival TimeBurst Time
P107
P224
P341
P454
Gantt Chart:
pseudo
|   P1 (7)   |  P2 (4)  | P3(1) |  P4 (4)  |
0             7          11     12         16
Tracing Table:
StepTimeReady QueueRunningEvent
10P1P1P1 starts
22P1's turn, P2 arrivesP1 continuesP2 ready
34P2, P3 arrivesP1 continuesP3 ready
45P2, P3, P4 arrivesP1 continuesP4 ready
57P2, P3, P4P2 startsP1 done
611P3, P4P3 startsP2 done
712P4P4 startsP3 done
816----P4 done
Average TAT = (7 + 9 + 8 + 11) / 4 = 8.75 Average WT = (0 + 5 + 7 + 7) / 4 = 4.75

3. Shortest-Job-First (SJF) — Non-preemptive

3.1 Intuition

The process with the smallest total burst time runs next. SJF is provably optimal in terms of average waiting time — but requires knowing burst times in advance (usually estimated using exponential averaging).

3.2 Worked Example

Using the same processes as Example 3 above:
ProcessArrival TimeBurst Time
P107
P224
P341
P454
Gantt Chart:
pseudo
|   P1 (7)   |  P3 (1)  |  P2 (4)  |  P4 (4)  |
0             7          8          12         16
Tracing Table:
StepTimeReady QueueRunningEvent
10P1P1P1 starts
22P2 (BT=4)P1 continuesP2 arrives
34P2(BT=4), P3(BT=1)P1 continuesP3 arrives
45P2(BT=4), P3(BT=1), P4(BT=4)P1 continuesP4 arrives
57P2(4), P3(1), P4(4)P3 startsP1 done; SJF picks P3
68P2(4), P4(4)P2 startsP3 done; SJF picks P2 (or P4)
712P4(4)P4 startsP2 done
816----P4 done
Calculations:
ProcessATBTStartEndTATWT
P1070770
P3417843
P224812106
P4541216117
Average TAT = (7 + 10 + 4 + 11) / 4 = 8.0 Average WT = (0 + 6 + 3 + 7) / 4 = 4.0 Compare with FCFS: WT was 4.75, now 4.0 — SJF is better!

4. Shortest Remaining Time First (SRTF) — Preemptive SJF

4.1 Intuition

SRTF is the preemptive version of SJF. When a new process arrives with a remaining burst time smaller than the currently running process, the scheduler preempts the current process.

4.2 Worked Example

ProcessArrival TimeBurst Time
P108
P214
P322
P431
Gantt Chart:
pseudo
| P1(1) | P2(1) | P3(1) | P4(1) | P2(3)  |   P1(7)   |
0       1       2       3       4        7          14
Tracing Table:
StepTimeEventReady Queue (remaining)Running
10P1 arrives--P1(8) starts
21P2 arrives (BT=4 < P1's 7)P1(7)P2(4) preempts
32P3 arrives (BT=2 < P2's 3)P1(7), P2(3)P3(2) preempts
43P4 arrives (BT=1 < P3's 1)P1(7), P2(3), P3(1)P4(1) preempts
54P4 doneP1(7), P2(3), P3(1)P3(1) resumes
65P3 doneP1(7), P2(3)P2(3) resumes
77P2 doneP1(7) (remaining 7)P1(7) resumes
814P1 done----
Calculations:
ProcessATBTEndTATWT
P10814146
P214762
P322531
P431410
Average TAT = (14 + 6 + 3 + 1) / 4 = 6.0 Average WT = (6 + 2 + 1 + 0) / 4 = 2.25 SRTF gives the best average waiting time of any algorithm, but can cause starvation (long processes may never run).

5. Common Pitfalls

Pitfall 1: Confusing preemptive and non-preemptive

Mistake: Applying SJF selection only at new arrivals (should also check remaining time for SRTF). Fix: In SRTF, after every new arrival, compare the new process's burst time with the remaining time of the running process.

Pitfall 2: Forgetting to update remaining time

Mistake: In SRTF tracing, treating remaining time as original burst time. Fix: Maintain a column showing remaining burst time after each time unit.

Pitfall 3: Ignoring arrival times

Mistake: Sorting all processes by burst time regardless of when they arrive. Fix: Only processes that have arrived by the current time can be scheduled.

6. 📐 Key Formulas / Concepts

ConceptFormulaNotes
Turnaround TimeTAT = C - ACompletion time - Arrival time
Waiting TimeWT = TAT - BTTurnaround time - Burst time
Response TimeRT = start - AFirst CPU time - Arrival time
Convoy EffectShort process waits behind long processProblem with FCFS
SJF OptimalitySJF minimizes average WTOnly if all processes arrive simultaneously

7. 📝 Practice Questions

Q1: Three processes P1 (BT=6), P2 (BT=8), P3 (BT=7) arrive at time 0. Compute average TAT and WT for FCFS in order P1, P2, P3.
Gantt: P1(0-6), P2(6-14), P3(14-21)
ProcessTATWT
P160
P2146
P32114
Avg TAT = (6+14+21)/3 = 13.67 Avg WT = (0+6+14)/3 = 6.67 Q2: Same processes as Q1, compute for SJF.
Order: P1(6), P3(7), P2(8)
ProcessTATWT
P160
P3136
P22113
Avg TAT = (6+13+21)/3 = 13.33 Avg WT = (0+6+13)/3 = 6.33 Q3: What is the convoy effect? Provide a concrete example.
Answer: The convoy effect describes the situation where a long CPU-bound process holds the CPU while many short processes wait. Example: P1(BT=10), P2(BT=1), P3(BT=1) with FCFS — average WT = (0+10+11)/3 = 7. If they ran in order P2, P3, P1, average WT = (0+1+2)/3 = 1. Q4: Why is SJF called "provably optimal"?
Answer: SJF minimizes average waiting time for a given set of processes assuming they all arrive at the same time. The proof shows that any swap of a short process with a longer one in the schedule increases the average waiting time. Q5: Processes P1(AT=0, BT=5), P2(AT=1, BT=3), P3(AT=2, BT=3), P4(AT=3, BT=1). Compute SRTF average WT.
Gantt: P1(0-1), P2(1-3), P3(3-4), P4(4 passes...). Let me trace properly:
TimeRunningReady Queue (rem)
0P1(5)--
1P2(3) [P1 has 4, P2 has 3 < 4]P1(4)
2P3(3) [P2 has 2, P3 has 3 > 2? No!]Wait, P2 ran from 1-2, remaining=2. P3(3) > P2(2), so P2 continues
2P2(2)P1(4), P3(3)
3P4(1) [P2 has 1, P4(1)=P2's 1, tie - let P4 run]P1(4), P2(1), P3(3)
4P2(1)P1(4), P3(3)
5P3(3)P1(4)
8P1(4)--
12----
WT: P1=7, P2=1, P3=3, P4=0 → Avg WT = 11/4 = 2.75

8. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.