One of the biggest steps forward in the design and operation of central processing units (CPUs) came when the designers of computer operating systems developed techniques to allow users to multitask. Multitasking in reference to computers is the practice of running two or more programs at the same time. From an end-user point of view, this might not seem complicated or farfetched, but it actually represents a considerable leap in system design. In multitasking, the CPU must juggle all of the operations for the different active programs, recording where it is with each program as the user toggles back and forth between them. Context switching is the process that allows the CPU to remember and restore states for a variety of active programs, enabling it to complete this juggling act.
During context switching, the CPU drops whatever program it is currently handling, storing the specific place it was in in that program so it can resume its progress later. To put that in perspective, context switching is a bit like reading multiple books at the same time, constantly switching back and forth between them while always remembering the page numbers for each. The "page number" information for programs during a context switching operation is held by the process control block (PCB). The PCB is also sometimes referred to as the "switchframe." This information is stored in memory within the actual CPU, until it is needed again.
Context switching occurs during three possible situations: interrupt handling, multitasking, and user switching. In interrupt handling, another program "interrupts" the current program while it is running. Once the CPU receives the interrupt, it performs a context switch to juggle between the running program and the program requesting immediate data. In multitasking, the CPU flops back and forth between the programs, giving a time slice of processing time to each and performing a context switching operation to change between the two. For some operating systems, a context switch is also performed during a user switch for the operating system, although this is not expressly required.
Either the operating system or the computer's hardware can control context switching. Some modern operating systems and are designed to control context switching through the operating system itself, ignoring any built-in hardware support for the procedure. This allows the operating system to save more information during a context switch, allowing it to preserve more information about the switch.