Context switch (source code)

= Context switch
{wiki}

A <context switch> transfers <CPU> execution from one <operating-system process> or <software thread> to another while preserving enough <execution context> for the suspended task to resume. The <operating-system kernel> saves the outgoing task's <processor registers>, including its resume <program counter> and <stack pointer>, and restores the incoming task's saved state. Hardware entry and software switching divide this work differently across architectures; the diagram groups them into logical save and load steps. During the transition the same <CPU> executes the <operating-system kernel>'s scheduler and switching code. https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf[Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 6, section 6.3: saving and restoring context]{ref}

For a schematic switch from A to B, let A's resume <program counter> be $p_A$, its <stack pointer> $u_A$, and one illustrative data <processor register> contain 7. B's saved values are $p_B,u_B,42$. Saving A retains $(p_A,u_A,7)$ in A's <execution context>; loading B establishes $(p_B,u_B,42)$ before B resumes. These are selected fields, not a complete architecture-specific <execution context>. Later restoring A recovers its 7 even though B has used the same physical <processor register> for 42.

\Image[/context-switch-state-transfer.png]
{title=One <CPU> over time: save A's <execution context>, run the scheduler, and restore B's <execution context>}

The useful invariant is that each suspended task retains its own resume state, so another task's use of the <processor registers> does not erase it. This is what makes repeated turns under <round-robin scheduling> possible. Saving an <execution context> does not copy the entire <address space>. Switching between <software threads> of one <operating-system process> can retain the same <address space>; switching between <operating-system processes> with different <address spaces> may also require changing the <memory management unit>'s translation context, such as the active <page table> and associated <address space identifier>, depending on the architecture. Their contents remain in <computer memory>. https://pages.cs.wisc.edu/~remzi/OSTEP/threads-intro.pdf[Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 26, introduction: thread contexts and shared address spaces]{ref}

Entering the <operating-system kernel> through an <interrupt>, <synchronous processor exception> or <system call> does not automatically imply a <context switch> to another task. The handler may return to the same task; <process scheduling> determines whether another runnable task should take over. A <context switch> consumes time, and a changed <memory cache> or <translation lookaside buffer> working environment can add subsequent costs. Those costs are excluded from the toy <process scheduling> timelines, which are calculations rather than <operating system> benchmarks. https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf[Arpaci-Dusseau and Arpaci-Dusseau, OSTEP chapter 6, section 6.3: continue the current process or switch]{ref}https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf[Arpaci-Dusseau and Arpaci-Dusseau, OSTEP chapter 7, section 7.7: direct and indirect switching costs]{ref}