How your computer fakes doing everything at once

How your computer fakes doing everything at once
Image: Intellect (generated)

In short: Your laptop feels like it is running dozens of apps simultaneously, but a single CPU core can only do one thing at a time. To create the illusion of multitasking, the operating system performs a high-speed shell game called a context switch, pausing and resuming tasks in milliseconds.

A context switch is the administrative process of saving the exact state of a running process or thread so it can be resumed later, while loading the state of another. This allows a single CPU to share its resources among multiple programs. Every time this happens, the operating system must perform 'administration'—saving and loading registers, memory maps, and updating various tables. The complexity of this task varies: switching between threads of the same process is generally faster than switching between unrelated processes because they share many resources like virtual memory maps.

Information regarding a process's status is typically held within a switchframe, also known as a Process Control Block (PCB). In the Linux kernel, for example, the system retrieves information about a new process from its PCB to load CPU state info, including the program counter and stack pointer. When transitioning between tasks using distinct memory spaces, performance drops because the Translation Lookaside Buffer (TLB) requires clearing. Until the cache is replenished, each following memory request triggers a 'miss', which produces noticeable delays.

Context switches are triggered by three main events. First, a process may make itself unrunnable, such as waiting for disk I/O. Second, a pre-emptive scheduler might force a switch using a timer interrupt to prevent one process from 'starving' others of CPU time. Third, hardware interrupts (like a finished disk read) can trigger the switch. Interestingly, while some older processors like the Intel 80386 offered hardware-level context switching via a Task State Segment (TSS), modern systems like Windows and Linux largely avoid it in favor of software switching because it is more selective and efficient.

Not all switches are created equal. While standard process switching has significant overhead, 'green threads' or goroutines in languages like Go can perform extremely lightweight context switches. In these cases, the switch is equivalent to a coroutine yield, making it only marginally more expensive than a simple subroutine call.

Opens the Intellect+ feed with this card first. Swipe up for the next one.

Sources

To cite this card, link to https://intellectplus.co.uk/app/science-context-switch.

How we source, check and correct cards: Editorial standards.

General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.

Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?

Related

Finding something worth knowing…