Context Switching

RoadmapsCore CS

Scenario

You're reading a book when your phone rings. You place a bookmark on the exact line you were reading, close the book, and answer the phone. When you're done, you open the book right to your bookmark and resume. That's a context switch.

Mental model

Context switching is a chef pausing one dish to prep another, saving the exact state of the first dish so it isn't ruined.

The chef notes down the temperature and ingredients added for Dish A (saving state to the PCB), cleans the station (flushing registers), sets up the notes for Dish B (loading new PCB), and starts cooking Dish B.

Explanation

In a multitasking operating system, multiple processes seem to run simultaneously, even on a single CPU core. This illusion is achieved through context switching: the process of pausing one running application, saving its exact state, and resuming another application so quickly that the user doesn't notice the interruption.

A process's 'context' is essentially its exact state at a specific microsecond in time. It includes the values in the CPU registers, the Program Counter (which tracks the next instruction to execute), memory management information, and open file descriptors. When a context switch occurs, the OS must freeze time for the current process by taking a snapshot of this context and saving it into the process's Process Control Block (PCB).

Once the old process's state is safely tucked away in memory, the OS scheduler selects a new process from the Ready queue. It retrieves the saved context from the new process's PCB and loads it back into the CPU registers. As soon as the Program Counter is restored, the CPU blindly resumes executing the new process exactly where it left off, entirely unaware that a switch occurred.

While crucial for multitasking, context switches are not free. They represent pure overhead—time spent switching rather than doing useful computational work. During a context switch, the CPU is entirely occupied with OS administrative tasks. Furthermore, swapping processes heavily disrupts CPU caches, leading to slower memory access immediately following a switch.

Key points

Common mistakes

Glossary

multitasking
The ability of a computer operating system to run multiple programs at what appears to be the exact same time by rapidly sharing the CPU between them.
context switching
The process where the CPU pauses the current program, saves its exact state, and loads the saved state of a different program to resume running it.
Program Counter
A special memory area in the CPU that constantly keeps track of exactly which instruction or line of code the program needs to execute next.

Recall questions

Questions & answers

Which of the following does NOT happen during a process context switch: saving registers, flushing the TLB, modifying the PCB, or executing user code?

Executing user code.

Approach: Recall that context switching is an administrative kernel task. User code execution is explicitly paused during this time.

Why is switching between two threads of the same process faster than switching between two different processes?

Threads share the same memory space, so the OS does not need to swap memory management information or flush the Translation Lookaside Buffer (TLB).

Approach: Highlight the shared resources of threads vs isolated resources of processes.

Continue learning

Previous: Process States and Transitions

Return to Core CS Roadmap