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:

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:

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:

  1. 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.
  2. 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.






  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License