The introduction

  • Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Is the purpose of the study in order to improve the quality of their thinking, is not learned directly xi you could learn to solve the problem, but by learning to optimize the process to solve the problem, more understanding problems, scientific leave no hidden trouble to solve the problem, so it is necessary to understand the context of CPU, for screening high CPU load groundwork.

As I mentioned in the previous CONTEXT switch of Cpu, the context switch of Cpu is caused by the multi-task of the system, which will cause the context switch degree of the process to be different due to the different types of tasks. Therefore, we divide the context switch of Cpu into the following categories according to the types of tasks: Process, thread, and interrupt context switches, the first two are easier to understand. What about interrupts? Don’t forget that our hardware can interrupt calls to programs like our hardware drivers by triggering signals.

CPU Context Switch

The system calls

Our Linux operating system divides the running space of a process into kernel space and user space. The user space has limited access to hardware resources. Only the kernel space can access all resources. Sorry, you don’t have direct access, but you can switch states via system calls.

From the point of view is that the CPU registers the position of the current process command first, and then save up and then put the position of the current process command to replace into kernel mode instruction to a new location, and then jump to kernel mode, when the system call ended, must continue to perform to switch back to user mode, the process of a system call, There were two CPU context switches, but they were performed in the same process.

So the system call process is usually called a privileged mode switch, not a context switch.

Process context switch

To understand the process context switch before, it is necessary to know who is managing the process, by the kernel, that process switch is also in the kernel state. Therefore, the process context includes not only virtual memory, stack, global variables and other user space resources, but also the state of the kernel stack, register and other kernel space. Therefore, before saving the kernel state and CPU registers of the current process, you need 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 switching timing of a process context switch

When do process context switches happen? This starts with process scheduling. Linux maintains a ready queue for each CPU, which is called a wait queue. Active processes, whether running or waiting, are sorted by priority and the amount of time they have waited for the CPU.

Triggering the process scheduling scenario

In order to ensure that the CPU is used evenly, the CPU time is divided into time slices. When each process executes, it is given an hourglass. When the countdown is over, the process is switched to another CPU, regardless of whether the process has finished executing.

Processes are suspended when system resources (such as memory) are insufficient, and other processes are scheduled to run by the system.

When a process actively suspends itself via a method such as sleep, it reschedule.

When a process with a higher priority is running, the process with a higher priority can run.

When hardware is interrupted, processes on the CPU are suspended by the interrupt and run interrupt service routines in the kernel