CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 5 of 6 · 2 min read

Hidden costs

The hidden costs

In plain words: Copying the registers is the cheap part. The bigger cost comes afterwards. A processor keeps recently used data and instructions in small, fast memories called caches. When process B starts, the caches are full of A's data. B's first accesses miss, and each miss goes to the slow main memory. Only after B has refilled the caches does it run at full speed.

A second structure, the translation lookaside buffer (TLB), caches the translations from a process's addresses to physical memory (Level 6). It too holds A's entries, and on many processors it must be cleared or tagged when the address space changes. So the true price of a switch is the direct work plus a period of slower running for the process that follows.

This is why switching is not free, and why the length of the time slice is a trade-off (Level 3):

Time slice Switch overhead (at 1 microsecond) Effect
1 ms 0.1 per cent Very responsive, a small loss
100 microseconds 1 per cent More switches, more cache misses
10 microseconds 10 per cent Most of the loss is lost cache contents

Common mistake

Measuring only the register copy and calling that the price of a switch. The cache and TLB effects often cost more than the copy itself.

Interview angle

Answer in your head first, then open it.

What is a context switch?

Saving the state of the running process in its PCB and loading the state of another process.

What is saved?

The program counter and registers, and the information about the address space.

Why is it expensive?

No user work happens during it, and the next process starts with cold caches and a cold TLB.

How does it differ from a mode switch?

A mode switch only changes the privilege level of the same process.

What triggers one?

A timer interrupt, a blocking system call, or a more important process becoming ready.

Check yourself: 1. Name one hidden cost after the switch. 2. At 2 microseconds per switch and a 4 ms slice, what share of the time goes to switching? 3. Why do threads in the same process switch faster than processes? (Hint: the address space.)

Words you will meet

Word Meaning
Context The processor values that belong to one process
Context switch Changing which process runs
Mode switch Changing between user and kernel mode
Overhead Time spent on switching instead of useful work
Cache A small, fast memory that holds recently used data
Time slice The longest time a process runs before it can be switched out

Quick notes

  • A context switch is the operation by which the kernel stops the process that is running on a processor, saves its context in its process control block, and loads the.
  • A mode switch is a change of the processor between user mode and kernel mode, caused by a system call, an interrupt or a trap, in which the process that is running stays the same.