Context switch
A context switch is the computing process of storing and restoring the state of a CPU (the context) such that multiple processes can share a single CPU resource. The context switch is an essential feature of a multitasking operating system. Context switches are usually computationally intensive and much of the design of operating systems is to optimize the use
of context switches.
Context switches usually involve the following steps, assuming process P1 is running on the CPU and process P2 is the next process to run on the CPU:
- Store the context of P1 somewhere in memory (usually on the stack of P1, or on the operating-system defined thread information structure). The context of the CPU will contain the value of registers such as the program counter, processor control and general purpose registers.
- Retrieve the context of P2 from memory (usually on the stack of P2, or on the system structure associated with P2).
- Return to the location contained in the program counter (the line of code at which the original process was interrupted).
Some processors, like the Intel 80386 and higher CPUs, have hardware support for context switches. When a task switch occurs (implicitly due to a CALL instruction, referring to a task gate, or explicitly due to an interrupt or exception) the CPU can automatically load the new state. While you would expect this to be rather fast, mainstream operating systems like Windows do not use this feature. This has two reasons:
- performance issues
- the hardware context saving process only saves the general purpose registers. It does not preserve floating point registers.
Some CPUs contain logic to allow several hardware contexts to simultaneously exist, eliminating the need to store and restore the CPU context to memory on context switch.
Context switching happens for 3 main reasons:
- A transition between user-mode and interrupt has occurred (for example, data comes in at the mouse port); this means that the CPU has to switch contexts.
- A task needs to be switched out of the CPU because it has exceeded its time slice. A preemptive multitasking operating system gives every task an amount of time that it can maximally execute, called its time slice. If a process does not voluntarily yield the CPU (for example, by performing an I/O operation), a timer interrupt fires, and the operating system schedules another process for execution instead. This ensures that the CPU cannot be monopolized by any one processor-intensive application.