Skip to the page

Operating systems · core

CPU scheduling and context switch

The scheduler picks which ready process runs next. A context switch saves one process state and loads another.

The notes

What you must know

  • FCFS runs in arrival order. A long job in front makes short jobs wait: the convoy effect.
  • Example with burst times 24, 3 and 3 ms: FCFS average wait is 17 ms; SJF (shortest job first) is 3 ms; round robin with a 4 ms quantum is about 5.67 ms.
  • SJF gives the lowest average waiting time, but the OS cannot know the next CPU burst; it can only estimate it from past bursts.
  • Round robin is fair and good for interactive systems. A quantum that is too small causes too many context switches.
  • Turnaround time = waiting time + burst time (for jobs that arrive at time zero). Do not mix up waiting and turnaround.
  • A context switch saves the state (program counter, registers, stack pointer, page table pointer) into the PCB and loads the next one from its PCB.
  • Triggers: a timer interrupt, a process blocking on I/O, a higher-priority process becoming ready, or a system call that waits.
  • Cost: no useful work during the switch, and caches and the TLB are cold afterwards. A thread switch in the same process is cheaper, because the address space stays the same.

The red pen

Common traps: mistake → fix

  • Mistake: SJF is used as it is in real systems

    Fix: the next burst length is unknown.

  • Mistake: The whole memory is copied to disk on each switch

    Fix: only the CPU state is saved in the PCB.

  • Mistake: Waiting time and turnaround time are the same

    Fix: turnaround includes the burst.

Easy to hard

Questions interviewers ask, easy to hard

  1. Three jobs of 24, 3 and 3 ms arrive together. Find the average waiting time under FCFS, SJF and round robin (quantum 4).
  2. What is the convoy effect?
  3. What happens in a context switch?
  4. Why is a thread switch cheaper than a process switch?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview