Processes, Context Switches and What They Cost
A process is a running program plus everything the kernel remembers about it: registers, memory map, open files. Swapping one for another means saving all of that and loading another set, which is cheap in instructions and expensive in lost cache.
What a learner can do afterwards
- List what the kernel has to save and restore on a context switch
- Follow a process through the ready, running and blocked states
- Explain why a switch costs more than the register copying suggests
1 · Read
A process is a program while it runs, plus everything the kernel remembers about it: its registers, its memory map, and its open files. Your music player is one process and your browser is another.
A context switch pauses one process and starts another. The kernel saves the old state, loads the saved state of the next process, and jumps back in. A process waits in ready, owns a processor in running, and stalls in blocked until an event like disk data arrives.
Copying registers is only a handful of quick moves, yet the switch still costs a lot. The cache is full of the old process data, so the new process fetches its own instructions slowly from main memory until the cache warms up again. That cold cache is the hidden bill.
Switching too often pays that bill again and again, so the processor refetches instead of working. In the tiny teaching system xv6 you can watch the scheduler save one set of registers, load the next, and resume.
A switch saves registers, maps, and files, then pays a cold cache bill, so too many switches slow the machine.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.