Discussion on the thread switching problem of linux

  • 2020-05-15 03:24:22
  • OfStack

The processor is always in one of the following states:

1, kernel state, running in the process context, the kernel on behalf of the process running in the kernel space;

2. Kernel mode, running in the interrupt context, the kernel running in the kernel space on behalf of the hardware;

3. User mode, running in user space;

The context of a process can be divided into three parts: user-level context, register context, and system-level context.

User-level context: body, data, user stack, and Shared storage;

Register context: general purpose register, program register (IP), processor status register (EFLAGS), stack pointer (ESP);

System-level context: process control block task_struct, memory management information (mm_struct, vm_area_struct, pgd, pte), kernel stack.

When a process is scheduled, a process switch is called a context switch (context switch). The operating system must switch all the information mentioned above in order for the new scheduled process to run. The mode switch made by the system call (mode switch). Mode switching is much easier and time saving than process switching, because the primary task of mode switching is simply switching the context of the process register. Each process in the system has its own context. A process that is running on the processor is called the current process (current). When the current process runs out of time slices or blocks while waiting for an event, the process scheduler needs to transfer the processor from the current process to another process. This process is called process switching. At this point, the called process becomes the current process. During process switching, the system stores the context of the current process in the specified memory region (TSS, the task state segment of the process), and then sets the context of the next process running on the processor to the current process. When a process is scheduled to run again using CPU, the system restores the context saved by the process. So, process switching is context switching. When the system kernel serves the user process, usually the process executes the kernel code through the system call, and then the execution state of the process transforms from the user state to the kernel state. However, at this point, the kernel is running to serve the user process, or the kernel is performing some service function on behalf of the current process. In this case, the kernel is still running as part 1 of the process, so the kernel is running in the process context. The kernel can access and modify the system data of a process while it is running in the process context. In addition, if the kernel is running in the process context and needs to wait for resources and devices, the system can block the current process.

Threads under Linux are essentially lightweight processes (light weighted process). When threads are generated, the corresponding process control structure will be generated, but this structure shares the same process memory space with the parent thread's process control structure. At the same time, the process control structure of the new thread will copy the same process information from the parent thread (process), such as open file list and signal blocking mask. Creating a thread is less expensive than creating a new process, because the newly created thread USES the address space of the current process. Switching between threads takes less time than switching between processes, because the latter does not include switching between address Spaces.

The principle of thread switching context switching is similar, except that the thread in the same address space does not need to switch MMU and so on, but only needs to switch the necessary CPU registers, so the thread switching is much faster than the process switching.


Related articles: