A context switch is the CPU's way of pretending many threads are running at once. This diagram shows the sequence: a timer interrupt fires, the kernel saves the running thread's registers onto its kernel stack, the scheduler picks the next thread by smallest vruntime, the page-table base (CR3) is reloaded and the TLB flushed if the thread belongs to a different process, the next thread's context is popped, and execution returns to user mode. The ordering matters because each step has a real cost: the TLB flush alone means the new thread starts with a cold translation cache, which is why switches measured in microseconds are still expensive at high rates. The done state: the new thread resumes mid-instruction, the old one parked until its turn.
Context Switch Sequence
Context switch sequence: timer interrupt, register save, CFS pick, CR3 reload, TLB flush, and resume in user mode.
The Runtime Theory Team06 stages
trace / request.md
readyThe scheduler tick — typically every 1 to 10 ms — interrupts the running thread. The CPU enters the kernel and the current execution is paused mid-instruction.