- CPU register: a small but fast memory built into the CPU.
- Program counter: is used to store the location of the CPU instruction that is being executed, or the location of the next instruction that will be executed.
These are the environments that the CPU must depend on before running any task, and are therefore also called CPU contexts.
CPU Context Switch
CPU context switch is to first save the CPU context of the previous task (i.e., CPU registers and program counters), then load the context of the new task into these registers and program counters, and finally jump to the new position indicated by the program counter to run the new task.
The saved context is stored in the system kernel and reloaded when the task is rescheduled. This ensures that the original state of the task remains intact and the task appears to be running continuously.
According to different tasks, CPU context switch can be divided into process context switch, thread context switch, interrupt context switch.
Process context switch
The running space of a process is divided into kernel space and user space. When a process runs in user space, it is called the user state of the process, and when it falls into kernel space, it is called the kernel state of the process. The transition from user mode to kernel mode is accomplished by system call.
- Kernel space (Ring 0) has the highest privileges and can directly access all resources;
- User space (Ring 3) can only access restricted resources, not hardware devices such as memory directly, and must be trapped in the kernel through system calls to access these privileged resources.
CPU privilege level diagram:
How is the CPU context switched during a system call?
- The original user mode instruction location in the CPU register needs to be saved first.
- Then, in order to execute the kernel code, the CPU register needs to be updated to the new location of the kernel instruction.
- The last step is to jump to kernel mode and run the kernel task.
- After the system call, the CPU register needs to restore the original saved user state
- Then switch to user space and continue running the process
So, in the process of one system call, there are actually two CPU context switches.
Tip: During the system call, resources in user mode such as virtual memory are not involved, and processes are not switched. This is not what we normally call a process context switch
- Process context switch: Switching from one process to another.
- System call procedure: The same process runs all the time. This is usually called a privileged mode switch, not a context switch.
What’s the difference between a process context switch and a system call?
Processes are managed and scheduled by the kernel, and process switching can only happen in kernel mode. The context of a process includes not only user space resources such as virtual memory, stack, global variables, but also the state of kernel space such as stack and register.
The difference is that the context switch of the process is one step more than the system call. Before saving the kernel state and CPU registers of the current process, it is necessary to save the virtual memory and stack of the process. After loading the kernel state of the next process, the virtual memory and user stack of the process need to be refreshed. The diagram below:
What happens if the process context switches more often?
It is easy for the CPU to spend a lot of time saving and restoring resources such as registers, kernel stacks, and virtual memory, which greatly reduces the time for the actual running process. The overall performance of the system is greatly reduced, which is also an important factor leading to the increase of average load.
Linux uses TLB (Translation Lookaside Buffer) to manage the mapping between virtual memory and physical memory. When the virtual memory is updated, the TLB also needs to be refreshed, slowing down memory access.
When is process context switched?
Context switching is required only when the process is scheduled. Linux maintains a ready queue for each CPU, sorting the active processes (that is, the processes that are running and waiting for the CPU) by priority and the amount of time they have waited for the CPU, and then selecting the processes that need the most CPU, that is, the processes that have the highest priority and wait for the CPU the most.
When is a process scheduled to run on the CPU?
- The process terminates, the CPU it used is released, and a new process is taken from the ready queue to run.
- To ensure that all processes can be scheduled fairly, CPU time is divided into time slices, which are then allocated to each process in turn. In this way, when a process runs out of time, it is suspended by the system and switched to another process that is waiting for the CPU.
- When the system resources (such as memory) are insufficient, a process can run only when the resources are sufficient. In this case, the process is suspended and the system schedules other processes to run.
- When a process actively suspends itself via a method such as sleep, it reschedule.
- When a process with a higher priority runs, the current process is suspended to ensure that the process with a higher priority runs.
- When a hardware interrupt occurs, processes on the CPU are suspended by the interrupt and run interrupt service routines in the kernel instead.
Thread context switch
The difference between thread and process
Threads are the basic unit of scheduling, and processes are the basic unit of resource ownership. To put it bluntly, the so-called task scheduling in the kernel is actually scheduled by threads; The process only provides virtual memory, global variables and other resources to the thread.
Two cases
- The two threads belong to different processes. At this point, since resources are not shared, the switching process is the same as a process context switch.
- The two threads belong to the same process. At this point, because the virtual memory is shared, so during the switch, the virtual memory resources remain unchanged, only need to switch the thread private data, registers and other non-shared data.
Even though it is a context switch, switching threads within a process consumes fewer resources than switching between multiple processes
Interrupt context switch
Interrupts the normal scheduling and execution of the interrupting process and in turn invokes interrupt handlers in response to device events. Interrupt context, in fact, only includes the state necessary for the execution of kernel-mode interrupt service program, including CPU registers, kernel stack, hardware interrupt parameters, etc.
Interrupt processing has higher priority than process for the same CPU, so interrupt context switching does not occur at the same time as process context switching. Similarly, since interrupts interrupt the scheduling and execution of normal processes, most interrupt handlers are short and concise in order to finish execution as quickly as possible.
👍 ❤️ Don’t get lost
The article continues to update every week, you can search wechat “ten minutes to learn programming” the first time to read and urge more, if this article is not bad, feel something if you support and recognition, is the biggest power of my creation, we will see the next article!