What Is Context Switching?

Context switch is a kind of environment switch.

Context switch is a kind of environment switch.
Context switching, sometimes called process switching or task switching, refers to the switching of the CPU from one process or thread to another process or thread.
Chinese name
Context switch
Foreign name
Context Switch
Nature
Switch

Introduction to context switching

Context switch or context switch
In the operating system, the CPU switching to another process needs to save the state of the current process and restore the state of the other process: the currently running task becomes ready (or suspended, deleted), and another selected ready task becomes the current task. Context switching includes saving the running environment of the current task and restoring the running environment of the task to be run.
The process context is represented by the PCB of the process (process control block, also called PCB, that is, task control block), which includes the process status, the value of the CPU register, and so on.
Normally, the current state of the CPU is saved by performing a state save, and then a state restore is performed to restart the operation.
Supporting multi-tasking is one of the biggest leaps in CPU design history. In computers, multitasking refers to running two or more programs simultaneously. From a user's perspective, this doesn't seem complicated or difficult to implement, but it is indeed a big leap in the history of computer design. In a multi-tasking system, the CPU needs to handle the operations of all programs, and when the user switches them back and forth, it needs to record where these programs are executed. Context switching is such a process that allows the CPU to record and restore the state of various running programs so that it can complete the switching operation.
During the context switch, the CPU stops processing the currently running program and saves the specific location where the current program is running so that it can continue to run later. From this perspective, context switching is a bit like we are reading several books at the same time. When switching back and forth between books, we need to remember the page number currently read by each book. In the program, the "page number" information during the context switch is saved in the process control block (PCB). PCBs are often referred to as "switchframes." The "page number" information is kept in the CPU's memory until they are used again.
Context switching may occur in three cases: interrupt processing, multitasking, and user mode switching. In interrupt processing, other programs "interrupted" the currently running program. When the CPU receives the interrupt request, it performs a context switch between the running program and the program that initiated the interrupt request. In multi-tasking, the CPU switches back and forth between different programs, each program has a corresponding processing time slice, and the CPU switches context between the two time slices. For some operating systems, a context switch is also performed when the user mode is switched, although this is not necessary.
The operating system or computer hardware supports context switching. Some modern operating systems control the context switch through the system itself. The entire switch process does not rely on the support of the hardware. Doing this allows the operating system to save more context switch information.

Consumption of context switch

Context switching is usually computationally intensive. In other words, it requires a considerable amount of processor time. In dozens of hundreds of switches per second, each switch takes nanoseconds of time. So, context switching means that the system consumes a lot of CPU time. In fact, it may be the most time-consuming operation in the operating system.

Context Switching Performance Impact

Context switching can have a negative impact on performance. Some context switches are more expensive than others; one of the more expensive context switches is Cross-Core Context Switch. A thread can run on a dedicated processor or across processors. Threads served by a single processor have Processor Affinity, which is more efficient. Preempting and scheduling threads on another processor core can cause cache misses, and as a result of cache misses and excessive context switching, local memory is accessed. In short, this is called "cross-core context switching." [1]

Context switch context switch and mode switch

Context switching can only occur in kernel mode. Kernel mode is a privileged mode of the CPU in which only the kernel runs and can access all memory and other system resources. Other programs, such as applications, initially run in user mode, but they can run part of the kernel's code through system calls. System calls in Unix-like systems refer to requests from active processes (processes running on the CPU) for services provided by the kernel, such as input / output (I / O) and process creation (creating a new process). I / O can be defined as any information flowing into or out of the CPU and main memory (RAM). In other words, any communication between a computer's CPU and memory with the user of the computer (via keyboard or mouse), storage device (hard disk or disk drive), and other computers is I / O.
The coexistence of these two modes (user mode and kernel mode) in Unix-like systems means that it is necessary for the CPU to switch to kernel mode when a system call occurs. This should be called a mode switch, not a context switch, because there is no change to the current process.
Context switching is a required feature in multitasking operating systems. A multi-tasking operating system is one in which multiple processes run on a CPU without disturbing each other, and appear to run simultaneously. This parallel illusion is due to context switching at high speed (tens to hundreds of times per second). A context switch occurs when a process voluntarily gives up its CPU time or runs out of time slices allocated by the system.
Context switches are sometimes triggered by hardware interrupts. A hardware interrupt refers to a signal sent by a hardware device (such as a keyboard, mouse, debug demodulator, and system clock) to the kernel. This signal indicates that an event (such as a key press, mouse movement, or data received from a network connection) has occurred.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?