Multitasking allows users to run multiple programs simultaneously, which requires the CPU to juggle operations and switch between programs using context switching. This process involves storing program information in the process control block and occurs during interrupt handling, multitasking, and user switching. The operating system or hardware can control context switching, with some modern operating systems allowing for more information to be saved during the switch.
One of the greatest advances in the design and operation of central processing units (CPUs) came when 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 perspective, this may not seem complicated or far-fetched, but it actually represents a major step forward in system design. In multitasking, the CPU has to juggle all the operations for the different active programs, registering where it is with each program as the user switches 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, allowing it to complete this juggling act.
During context switching, the CPU kills whatever program it is currently managing, storing the specific location it was in that program so it can resume its progress later. To put that into perspective, context switching is a bit like reading multiple books at once, constantly switching back and forth between them while always remembering the page numbers for each. The “page number” information for programs during a context switch operation is maintained by the process control block (PCB). The PCB is also sometimes referred to as a “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 that requires immediate data. In multitasking, the CPU switches back and forth between programs, assigning each a slice of processing time and performing a context switching operation to switch 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 explicitly required.
The operating system or computer hardware can control context switching. Some modern operating systems are designed to control context switching through the operating system itself, overriding any built-in hardware support for the procedure. This allows the operating system to save more information during a context switch, allowing it to retain more information about the switch.
Protect your devices with Threat Protection by NordVPN