Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Context switch

From Wikipedia, the free encyclopedia
Switch between processes or tasks on a computer
This article is about computer task switching. For the term in human cognition, seeHuman multitasking.

Incomputing, acontext switch is the process of storing the state of aprocess orthread, so that it can be restored and resumeexecution at a later point, and then restoring a different, previously saved, state.[1] This allows multiple processes to share a singlecentral processing unit (CPU), and is an essential feature of a multiprogramming ormultitasking operating system. In a traditional CPU, each process – a program in execution – uses the various CPU registers to store data and hold the current state of the running process. However, in a multitasking operating system, the operating system switches between processes or threads to allow the execution of multiple processes simultaneously.[2] For every switch, the operating system must save the state of the currently running process, followed by loading the next process state, which will run on the CPU. This sequence of operations that stores the state of the running process and loads the following running process is called a context switch.

The precise meaning of the phrase "context switch" varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of aninterrupt, such as when a task needs to accessdisk storage, freeing up CPU time for other tasks. Some operating systems also require a context switch to move betweenuser mode andkernel mode tasks. The process of context switching can have a negative impact on system performance.[3]: 28 

Cost

[edit]

Context switches are usually computationally intensive, and much of the design of operating systems is to optimize the use of context switches. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc. What is actually involved in a context switch depends on the architectures, operating systems, and the number of resources shared (threads that belong to the same process share many resources compared to unrelated non-cooperating processes).

For example, in theLinux kernel, context switching involves loading the correspondingprocess control block (PCB) stored in the PCB table in the kernel stack to retrieve information about the state of the new process. CPU state information including the registers,stack pointer, andprogram counter as well as memory management information likesegmentation tables andpage tables (unless the old process shares the memory with the new) are loaded from the PCB for the new process. To avoid incorrect address translation in the case of the previous and current processes using different memory, thetranslation lookaside buffer (TLB) must be flushed. This negatively affects performance because every memory reference to the TLB will be a miss because it is empty after most context switches.[4][5]

Furthermore, analogous context switching happens betweenuser threads, notablygreen threads, and is often very lightweight, saving and restoring minimal context. In extreme cases, such as switching betweengoroutines inGo, a context switch is equivalent to acoroutine yield, which is only marginally more expensive than asubroutine call.

Switching cases

[edit]

There are three potential triggers for a context switch:

Multitasking

[edit]

Most commonly, within somescheduling scheme, one process must be switched out of the CPU so another process can run. This context switch can be triggered by the process making itself unrunnable, such as by waiting for anI/O orsynchronization operation to complete. On apre-emptive multitasking system, the scheduler may also switch out processes that are still runnable. To prevent other processes from being starved of CPU time, pre-emptive schedulers often configure a timer interrupt to fire when a process exceeds itstime slice. This interrupt ensures that the scheduler will gain control to perform a context switch.

Interrupt handling

[edit]

Modern architectures areinterrupt driven. This means that if the CPU requests data from a disk, for example, it does not need tobusy-wait until the read is over; it can issue the request (to the I/O device) and continue with some other task. When the read is over, the CPU can beinterrupted (by a hardware in this case, which sends interrupt request toPIC) and presented with the read. For interrupts, a program called aninterrupt handler is installed, and it is the interrupt handler that handles the interrupt from the disk.

When an interrupt occurs, the hardware automatically switches a part of the context (at least enough to allow the handler to return to the interrupted code). The handler may save additional context, depending on details of the particular hardware and software designs. Often only a minimal part of the context is changed in order to minimize the amount of time spent handling the interrupt. Thekernel does not spawn or schedule a special process to handle interrupts, but instead the handler executes in the (often partial) context established at the beginning of interrupt handling. Once interrupt servicing is complete, the context in effect before the interrupt occurred is restored so that the interrupted process can resume execution in its proper state.

User and kernel mode switching

[edit]

When the system transitions betweenuser mode andkernel mode, a context switch is not necessary; amode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.

Steps

[edit]

The state of the currently executing process must be saved so it can be restored when rescheduled for execution.

The process state includes all the registers that the process may be using, especially theprogram counter, plus any other operating system specific data that may be necessary. This is usually stored in a data structure called aprocess control block (PCB) orswitchframe.

The PCB might be stored on a per-processstack in kernel memory (as opposed to the user-modecall stack), or there may be some specific operating system-defined data structure for this information. Ahandle to the PCB is added to a queue of processes that are ready to run, often called theready queue.

Since the operating system has effectively suspended the execution of one process, it can then switch context by choosing a process from the ready queue and restoring its PCB. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the chosen process. Process and thread priority can influence which process is chosen from the ready queue (i.e., it may be apriority queue).

Examples

[edit]

The details vary depending on the architecture and operating system, but these are common scenarios.

No context switch needed

[edit]

Considering a general arithmetic addition operation A = B+1. The instruction is stored in theinstruction register and theprogram counter is incremented. A and B are read from memory and are stored in registers R1, R2 respectively. In this case, B+1 is calculated and written in R1 as the final answer. This operation as there are sequential reads and writes and there's no waits forfunction calls used, hence no context switch/wait takes place in this case.

Context switch caused by interrupt

[edit]

Suppose a process A is running and a timer interrupt occurs. The user registers — program counter, stack pointer, and status register — of process A are then implicitly saved by the CPU onto the kernel stack of A. Then, the hardware switches to kernel mode and jumps into interrupt handler for the operating system to take over. Then the operating system calls theswitch() routine to first save the general-purpose user registers of A onto A's kernel stack, then it saves A's current kernel register values into the PCB of A, restores kernel registers from the PCB of process B, and switches context, that is, changes kernel stack pointer to point to the kernel stack of process B. The operating system then returns from interrupt. The hardware then loads user registers from B's kernel stack, switches to user mode, and starts running process B from B's program counter.[6]

Performance

[edit]

Context switching itself has a cost in performance, due to running thetask scheduler, TLB flushes, and indirectly due to sharing theCPU cache between multiple tasks.[7] Switching between threads of a single process can be faster than between two separate processes because threads share the samevirtual memory maps, so a TLB flush is not necessary.[8]

The time to switch between two separate processes is called theprocess switching latency. The time to switch between two threads of the same process is called thethread switching latency. The time from when a hardware interrupt is generated to when the interrupt is serviced is called theinterrupt latency.

Switching between two processes in asingle address space operating system can be faster than switching between two processes in an operating system with private per-process address spaces.[9]

Hardware vs. software

[edit]

Context switching can be performed primarily by software or hardware. Some processors, like theIntel 80386 and its successors,[10] have hardware support for context switches, by making use of a special data segment designated thetask state segment (TSS). A task switch can be explicitly triggered with a CALL or JMP instruction targeted at a TSS descriptor in theglobal descriptor table. It can occur implicitly when an interrupt or exception is triggered if there is atask gate in theinterrupt descriptor table (IDT). When a task switch occurs, the CPU can automatically load the new state from the TSS.

As with other tasks performed in hardware, one would expect this to be rather fast; however, mainstream operating systems, includingWindows andLinux,[11] do not use this feature. This is mainly due to two reasons:

  • Hardware context switching does not save all the registers (only general-purpose registers, notfloating-point registers — although theTS bit is automatically turned on in theCR0control register, resulting in a fault when executing floating-pointinstructions and giving the OS the opportunity to save and restore the floating-point state as needed).
  • Associated performance issues, e.g., software context switching can be selective and store only those registers that need storing, whereas hardware context switching stores nearly all registers whether they are required or not.

See also

[edit]

References

[edit]
  1. ^Douglas Comer; Timmothy V. Fossum (1988). "4 Scheduling and Context Switching".Operating System Design. Vol. I: The XINU Approach (PC Edition). Prentice Hall. p. 67.ISBN 0-13-638180-4.Context switching lies at the heart of the process juggling act. It consists of stopping the current computation, saving enough information so it may be restarted later, and restarting another process.
  2. ^Or sequentially so rapidly that they appear to be simultaneous.
  3. ^Tanenbaum, Andrew S.; Bos, Herbert (March 20, 2014).Modern Operating Systems (4th ed.). Pearson.ISBN 978-0133591620.
  4. ^IA-64 Linux Kernel: Design and Implementation,4.7 Switching Address Spaces
  5. ^Operating Systems, 5.6 The Context Switch,p. 118
  6. ^Arpaci-Dusseau, Remzi; Arpaci-Dusseau, Andrea (2015).Operating Systems: Three Easy Pieces.Archived from the original on 25 July 2016. Retrieved25 July 2016.
  7. ^Chuanpeng Li; Chen Ding; Kai Shen.Quantifying The Cost of Context Switch(PDF). ACMFederated Computing Research Conference, San Diego, 13-14 June 2007.Archived(PDF) from the original on 2017-08-13.
  8. ^Ulrich Drepper (9 October 2014)."Memory part 3: Virtual Memory".LWN.net.
  9. ^D.L. Sims."Multiple and single address spaces: towards a middle ground".1993.doi:10.1109/IWOOOS.1993.324906
  10. ^"Context Switch definition". Linfo.org. Archived fromthe original on 2010-02-18. Retrieved2013-09-08.
  11. ^Bovet, Daniel Pierre; Cesati, Marco (2006).Understanding the Linux Kernel, Third Edition.O'Reilly Media. p. 104.ISBN 978-0-596-00565-8. Retrieved2009-11-23.

External links

[edit]
General
Variants
Kernel
Architectures
Components
Process management
Concepts
Scheduling
algorithms
Memory management,
resource protection
Storage access,
file systems
Supporting concepts
Retrieved from "https://en.wikipedia.org/w/index.php?title=Context_switch&oldid=1277193875"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp