Context Switch Details

A context switch (sometimes called a process switch or task switch) refers to the switching of the  CPU  from one process or thread to another.
A process (sometimes called a task) is an instance of a program running . In  Linux  systems, threads are lightweight processes that can run in parallel and share the same address space (a region of memory) and other resources as their parent process (the process that created them). Context refers to the contents of the CPU registers and program counter at a point in time. Registers are small but fast pieces of memory inside the CPU (as opposed to the relatively slow RAM main memory outside the CPU). Registers increase the speed at which computer programs run by providing fast access to frequently used values, usually intermediate values ​​of operations. The program counter is a special register used to indicate the position in the instruction sequence that the CPU is executing, and the stored value is the position of the instruction being executed or the position of the next instruction to be executed, depending on the specific system. To describe in a little detail, context switching can be considered as the kernel (the core of the operating system) performing the following activities on the CPU for processes (including threads): (1) Suspend a process and change the state (context) of this process in the CPU Store somewhere in memory, (2) retrieve the context of the next process in memory and restore it in the CPU's registers, (3) jump to the location pointed to by the program counter (i.e. jump to where the process was interrupted line of code) to resume the process.


A context switch is sometimes described as the kernel suspends the process currently executing on the CPU and then resumes execution of one of the many previously suspended processes. While this helps to clarify the concept, the sentence itself can be a little confusing. Because by definition, we know that a process refers to an instance of a program running. So it may be more appropriate to say that the operation of a process is suspended.

context switching and mode switching

Context switching can only happen in kernel mode . Kernel mode is a privileged mode of the CPU in which only the kernel runs and has access to all memory and other system resources. Other programs, such as applications, initially run in userland , but they can run parts of the kernel's code through system calls. A system call in Unix-like systems refers to an active process (a process running on the CPU) making a request 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). That is, any communication between a computer's CPU and memory with the computer's user (via keyboard or mouse), storage devices (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 rather than a context switch, since the current process is not changed.
Context switching is a required feature in multitasking operating systems. A multitasking operating system means that multiple processes run on one CPU without disturbing each other, and appear to be running at the same time. This illusion of parallelism is due to context switching at high speed (tens or hundreds of times per second). A context switch occurs when a process voluntarily relinquishes its CPU time or when the system-allocated time slice runs out.
Context switches are also sometimes triggered by hardware interrupts. A hardware interrupt is a signal sent to the kernel by a hardware device (eg, keyboard, mouse, debugger, system clock) to indicate that an event (eg, key press, mouse movement, data received from a network connection) has occurred.
Intel's 80386 and higher CPUs support hardware context switching. However, most modern operating systems implement context switching in software rather than using hardware context switching, which can run on any CPU. At the same time, use software context switching to try and get better performance. Software context switching was first implemented in Linux 2.4.
A major advantage claimed to be software context switching is that the hardware mechanism saves almost all of the CPU state, and the software can selectively save and reload the parts that need to be saved. However, there is still some doubt as to how important this behavior is to improve the performance of context switching. Its proponents also claim that software context switching has the potential to improve switching code, which helps improve the effectiveness of data being loaded, thereby further improving performance.

The consumption of context switching

Context switching is usually computationally intensive. That is, it requires considerable processor time, in the order of nanoseconds for each switch in dozens or hundreds of switches per second. So, context switching means a lot of CPU time to the system, and in fact, may be the most time-consuming operation in the operating system.
Linux has many advantages over other operating systems (including other Unix-like systems), one of which is that context switching and mode switching consume very little time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326018081&siteId=291194637