Skip to content
Chethan Yadav_
Operating Systems

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

CriterionMeaningWant
CPU utilization% of time CPU is busyMax
ThroughputProcesses finished per unit timeMax
Turnaround TimeCompletion time minus arrival timeMin
Waiting TimeTime spent in the ready queueMin
Response TimeFirst time on CPU minus arrival timeMin

Formulas to memorize:

Turnaround Time (TAT) = Completion Time - Arrival Time
Waiting Time (WT)     = TAT - Burst Time
Response Time (RT)    = First Run Time - Arrival Time

FCFS

First Come First Served: run processes in arrival order. Non-preemptive, implemented with a FIFO queue.

Example (all arrive at 0):

ProcessBurst
P124
P23
P33
Gantt: | P1 (0-24) | P2 (24-27) | P3 (27-30) |
WT: P1=0, P2=24, P3=27   Average WT = 17

If 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:

ProcessArrivalBurst
P108
P214
P329
P435
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.5

Priority 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):

ProcessBurst
P124
P23
P33
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.67

Time 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:

  1. New process enters the top queue (small quantum).
  2. If it uses its whole quantum (CPU-bound), demote it to a lower queue with a larger quantum.
  3. If it gives up the CPU early (I/O-bound, interactive), it stays high.
  4. 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

AlgorithmPreemptiveStarvationNotes
FCFSNoNoConvoy effect
SJFNoYesOptimal avg WT, needs burst prediction
SRTFYesYesOptimal preemptive version
PriorityEitherYesFix with aging
Round RobinYesNoBest response time, quantum tuning
MLFQYesPrevented by boostGeneral 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.