Context switch

switch between processes or tasks on a computer

A context switch is the computing process of saving and restoring the state (context) of a CPU 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 time consuming and much of the design of operating systems is to minimize the time of context switches.

A context switch can mean a register context switch, a task context switch, a thread context switch, or a process context switch. What will be switched is determined by the processor and the operating system.

The scheduler is the part of the operating systems that manage context switching, it perform context switching in one of the following conditions:

  1. Multitasking: One process needs to be switched out of (termed "yield" which means "give up") the CPU so another process can run. Within a preemptive multitasking operating system, the scheduler allows every task (according to its priority level) to run for some certain amount of time, called its time slice where a timer interrupt triggers the operating system to schedule another process for execution instead.
    If a process will wait for one of the computer resources or will perform an I/O operation, the operating system schedules another process for execution instead.
  2. Interrupt handling: Some CPU architectures (like the Intel x86 architecture) are interrupt driven. When an interrupt occurs, the scheduler calls its interrupt handler in order to serve the interrupt after switching contexts; the scheduler suspended the currently running process till executing the interrupt handler.
  3. User and kernel mode switching: When a transition between user mode and kernel mode is required in an operating system, a context switch is not necessary; a mode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.

Context switching can be performed primarily by software or hardware. Some CPUs have hardware support for context switches, else if not, it is performed totally by the operating system software. In a context switch, the state of a process must be saved somehow before running another process, so that, the scheduler resume the execution of the process from the point it was suspended; after restoring its complete state before running it again.

Other websites change