OS/Lesson 4 of 9/6 min read
CPU Scheduling
Scheduling criteria, preemptive vs non-preemptive, FCFS, SJF, SRTF, priority, Round Robin, multilevel queues, with worked examples.
On this page
Why CPU Scheduling
When several processes are ready, the short-term scheduler decides which one runs next. The dispatcher then performs the context switch. The goal is to keep the CPU busy while being fair and responsive.
Processes alternate between CPU bursts and I/O bursts. I/O-bound processes have many short CPU bursts; CPU-bound processes have few long ones.
Preemptive Scheduling vs Non-Preemptive
- Non-preemptive: once a process gets the CPU it keeps it until it finishes or blocks. Simple, but one long job can hold everyone up.
- Preemptive: the OS can take the CPU away (timer interrupt, higher-priority arrival). Better responsiveness, but needs care with shared data.
Scheduling criteria
| Criterion | Meaning | Want |
|---|---|---|
| CPU utilization | % of time CPU is busy | Max |
| Throughput | Processes finished per unit time | Max |
| Turnaround Time | Completion time minus arrival time | Min |
| Waiting Time | Time spent in the ready queue | Min |
| Response Time | First time on CPU minus arrival time | Min |
Formulas to memorize:
Turnaround Time (TAT) = Completion Time - Arrival Time
Waiting Time (WT) = TAT - Burst Time
Response Time (RT) = First Run Time - Arrival TimeFCFS
First Come First Served: run processes in arrival order. Non-preemptive, implemented with a FIFO queue.
Example (all arrive at 0):
| Process | Burst |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
Gantt: | P1 (0-24) | P2 (24-27) | P3 (27-30) |
WT: P1=0, P2=24, P3=27 Average WT = 17If the order were P2, P3, P1, average WT would be (0 + 3 + 6) / 3 = 3. Order matters a lot.
Convoy Effect
Short processes stuck behind one long CPU-bound process, like cars behind a truck. It lowers CPU and device utilization. This is FCFS's main weakness.
Shortest Job First
SJF picks the process with the smallest next CPU burst. Non-preemptive.
- Provably optimal for minimum average waiting time.
- Problem: the next burst length is unknown. OS estimates it with exponential averaging:
t(n+1) = a * actual(n) + (1 - a) * t(n). - Can cause starvation of long jobs.
SRTF
Shortest Remaining Time First is preemptive SJF: if a new process arrives with a burst shorter than the running process's remaining time, it preempts.
Example:
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
Gantt: | P1 0-1 | P2 1-5 | P4 5-10 | P1 10-17 | P3 17-26 |
WT: P1 = 17-0-8 = 9, P2 = 5-1-4 = 0, P3 = 26-2-9 = 15, P4 = 10-3-5 = 2
Average WT = 26 / 4 = 6.5Priority Scheduling
Each process has a priority; the highest priority runs first (often a smaller number means higher priority). Can be preemptive or not. SJF is priority scheduling where priority = predicted burst.
Starvation
Starvation (indefinite blocking): a low-priority process may never run if higher-priority ones keep arriving.
Aging
Aging fixes starvation: gradually increase the priority of processes that wait a long time, so eventually they run.
Round Robin
Round Robin (RR) is FCFS with preemption: each process gets a fixed time quantum; when it expires the process goes to the back of the ready queue. Designed for time-sharing systems.
Example (quantum = 4, all arrive at 0):
| Process | Burst |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
Gantt: | P1 0-4 | P2 4-7 | P3 7-10 | P1 10-14 | P1 ... 30 |
WT: P1 = 30-24 = 6, P2 = 4, P3 = 7 Average WT = 5.67Time Quantum choice
- Very large quantum: RR behaves like FCFS.
- Very small quantum: too many context switches, overhead dominates.
- Rule of thumb: about 80% of CPU bursts should finish within one quantum. Typical values 10 to 100 ms.
RR has good response time and no starvation, but average turnaround is usually worse than SJF.
Multilevel Queue
Ready queue split into separate queues (system, interactive, batch), each with its own algorithm (RR for interactive, FCFS for batch). Queues have fixed priority between them. A process is permanently assigned to one queue, so lower queues can starve.
Multilevel Feedback Queue
MLFQ lets processes move between queues based on behavior:
- New process enters the top queue (small quantum).
- If it uses its whole quantum (CPU-bound), demote it to a lower queue with a larger quantum.
- If it gives up the CPU early (I/O-bound, interactive), it stays high.
- Periodically boost everything to the top to prevent starvation (a form of aging).
MLFQ approximates SJF without knowing burst times. Most real schedulers derive from it. Linux uses CFS (Completely Fair Scheduler), which tracks virtual runtime in a red-black tree and always runs the task that has had the least CPU.
Comparison
| Algorithm | Preemptive | Starvation | Notes |
|---|---|---|---|
| FCFS | No | No | Convoy effect |
| SJF | No | Yes | Optimal avg WT, needs burst prediction |
| SRTF | Yes | Yes | Optimal preemptive version |
| Priority | Either | Yes | Fix with aging |
| Round Robin | Yes | No | Best response time, quantum tuning |
| MLFQ | Yes | Prevented by boost | General purpose |
Quick revision
- TAT = CT - AT; WT = TAT - BT.
- SJF/SRTF minimize average waiting time.
- RR minimizes response time; quantum too small means overhead.
- Starvation is fixed with aging.