【Linux Kernel 6.1 Code Analysis】- Introduction to Process Management

Table of contents

The concept of process and thread (kernel thread and user thread)

3 basic states of a process

7 basic states after introducing suspend

Linux Kernel 6.1 - 8 detailed states of a process

Process Control Block PCB

SMP architecture


The concept of process and thread (kernel thread and user thread)

A process is a running program entity, and includes all system resources occupied by this running program, such as CPU ( registers ), IO, memory , network resources, etc. Each process has a process control block (PCB) in the kernel to maintain the basic information and running status of the process.
Thread is the basic unit of system scheduling. There can be multiple threads in a process, and they share process resources.

From the perspective of Linux, threads and processes can also be understood in this way:

A process has a dedicated system stack space and an independent user space. If a process needs to share user space with other processes, the process can be called a user thread. If a process has no user space at all, it can be called it for "kernel threads".

3 basic states of a process

Ready (Ready) state:

Through Ready, we can see that the process in this state is ready to run. At this time, the process has allocated all the necessary resources except the CPU, and it can be executed immediately only by obtaining the CPU.

Use one sentence to describe the process in the ready state: everything is ready, only the CPU is owed.

In addition, there is a concept of a ready queue, in which the processes in the ready state are all in this queue, waiting for the scheduling (allocation of CPU) of the scheduler.

Execution (Running) status:

Running, running, the process in this state has obtained the CPU and is executing.

For this state, in a single-CPU OS, only one process can be in this state at a time, while in a multi-CPU OS, there can be multiple (not exceeding the number of CPUs) processes in the execution state at the same time.

Blocking (Block) status:

Blocking, Baidu interprets as: obstacles that cannot pass through, and cannot be unblocked. The process in this state is temporarily unable to continue execution due to some event that needs to wait (I/O request, failure to apply for cache, waiting to receive data, etc.) during execution.

We know that in a multi-channel batch processing system, the process needs to release the processor at this time, and a new ready process is selected for execution. Corresponding to the ready queue, the processes in the blocked state are all in the blocking queue. In some OS, for the purpose of improving efficiency, there will be multiple blocking queues according to the cause of blocking.

7 basic states after introducing suspend

reason for being suspended

In order to meet the needs of the system and users to observe and analyze the process, an important operation on the process—Suspend (Suspend) is also introduced. When this operation is performed on a process, the process will be suspended, which means The process needs to release the memory and transfer it to the hard disk (external memory), which also means that the process is in a static state at this time and cannot be scheduled or executed. When the process is activated (Active), the process can be reimported from the external memory to the memory.

  1. End user's request : When an end user finds a suspicious problem during the running of his own program, he wishes to suspend the running of his own program to stop it so that the user can study its execution or modify the program;
  2. Request from the parent process : Sometimes the parent process wishes to suspend one of its child processes in order to examine and modify the child process, or to coordinate activities among the child processes;
  3. The need for load adjustment : when the workload in the real-time system is heavy and may affect the control of real-time tasks, the system can suspend some unimportant processes to ensure the normal operation of the system;
  4. Needs of the operating system : The operating system sometimes wants to suspend certain processes in order to check on-the-fly resource usage or for accounting purposes.

4 new states

  1. Active ready (Readya) : equivalent to the ready state, at this time the process can accept scheduling, and can directly transfer to the execution state after obtaining the processor;
  2. Readys (Readys) : The process is transferred to external memory and cannot be scheduled;
  3. Active blocking (Blockeda) : equivalent to blocking state, when the waiting event occurs, it can change from active blocking to active ready state;
  4. Static blocking (Blockeds) : The process can still wait for the event, when the event occurs, the state changes from static blocking to static ready state.

Linux Kernel 6.1 - 8 detailed states of a process

Running status (TASK_RUNNING):

The process is currently running, or waiting to be scheduled in the run queue.

Interruptible blocking (sleeping) state (TASK_INTERUPTIBLE):

A process is in a blocked (sleeping) state, waiting for some event to occur or to be able to occupy some resource. A process in this state can be interrupted by a signal. After receiving a signal or being awakened by an explicit wakeup call (such as calling wake_up series macros: wake_up, wake_up_interruptible, etc.), the process will change to the TASK_RUNNING state.

Uninterruptible blocking (sleeping) state (TASK_UNINTERUPTIBLE):

This process state is similar to the interruptible blocked state, except that it does not handle signals, and processes that deliver signals to this state cannot change its state. This state is useful in some specific situations (where a process must wait until some event occurs that cannot be interrupted). A process is woken up by an explicit wakeup call only when the event it is waiting for occurs.

This state is also generally referred to as the disk sleep state, and usually occurs when the disk is written.

Terminable blocking (sleeping) state (TASK_KILLABLE):

This is a new process state introduced by Linux kernel 2.6.25. The operating mechanism of the state is similar to the uninterruptible blocking state, except that the process in this state can respond to fatal signals.

It serves as an alternative to uninterruptible blocking states, which are efficient but may not terminate, and interruptible blocking states, which are easy to wake up but less safe.

Suspended state (TASK_STOPPED):

The execution of the process is suspended. When the process receives signals such as SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, etc., it will enter the suspended state.

Tracking status (TASK_TRACED):

Execution of the process is suspended by the debugger. When a process is being monitored by another process (such as a debugger using the ptrace() system call to monitor a test program), any signal can put the process into the trace state.

Zombie state (EXIT_ZOMBIE):

When the process finishes running, the parent process has not yet used system calls such as the wait function family (such as the waitpid() function) to "collect the corpse", that is, it waits for the parent process to destroy it. The process "corpse" in this state has given up almost all memory space, does not have any executable code, and cannot be scheduled. It only reserves a position in the process list to record the exit status of the process and other information for other processes. collect.

Zombie withdrawal status (EXIT_DEAD):

This is the final state. After the parent process calls the wait function family to "collect the corpse", the process is completely deleted by the system.

Process Control Block PCB

contains information

  1. Process identifier : Used to uniquely identify a process. A process usually contains two kinds of identifiers: external identifiers and internal identifiers. The external identifier is generally provided by the creator (user) for easy memory; the internal identifier is for the convenience of the OS to use the process, usually the Pid seen in the Linux system;
  2. CPU state : CPU state information is also called the context of the processor, which is mainly composed of the contents of various registers of the CPU (general registers, instruction counters, program status word PSW, user stack pointer). During the execution of the process, many information being processed are stored in registers. If a switch needs to occur, the information needs to be saved in the PCB of the process, so that the state of the CPU can be quickly restored when it is executed again;
  3. Process scheduling information : When the OS schedules (selects a process from the ready queue to allocate the CPU), it must know the status of the process and related scheduling information, mainly including: process status, priority, and other information required for process scheduling (waiting time, time already executed, etc.), event (time waiting to happen, i.e. reason for blocking);
  4. Process control information : only information necessary for process control, mainly including: address of program and data (first address of memory or external memory), process synchronization and communication mechanism, resource list (all required during process running) Resources, except for the CPU, there is also a list of resources that have been allocated to the process, mainly used to avoid deadlocks), link pointers (pointing to the first address of the PCB of the next process in the queue where this PCB is located).

effect

  1. As a symbol of the basic unit of independent operation: when a program (including data) is configured with a PCB, it means that it has become a process, a basic unit that can run independently in a multi-program environment and is legal, and has the ability to obtain Entitlement to OS services. This is why when the system creates a process, it needs to create a PCB for it, and bind it one-to-one with the process. The system perceives the process according to the PCB. When the PCB is revoked and returned to the OS, the process will also die. up;
  2. The way to realize intermittent operation: This is also a basic characteristic of the process—asynchronous, because the context information of the process when it is executed on the CPU is saved in the PCB, so when the process is scheduled for execution again, its CPU can be quickly restored. information.
  3. Provide the information needed for process management: During the entire life cycle of the process, the OS controls and manages the process according to the PCB.
  4. Provide the information required for process scheduling: OS inserts the inheritance into the corresponding queue according to the state information of the process stored in the PCB, and schedules according to the priority and waiting events saved in the PCB;
  5. Realize synchronization and communication with other processes: There are areas or communication queue pointers for communicating with the realization process in the PCB.

SMP architecture

concept

Symmetrical multi-processor structure, the English name is "Symmetrical Multi-Processing", referred to as SMP;

SMP is also known as UMA, the full name is "Uniform Memory Access", and the Chinese name is "Unified Memory Access Architecture";

In the "symmetrical multi-processor structure" system, all CPU processors are equal in status, generally referring to multiple CPUs running on server equipment, without primary/subordinate/subordinate relationship, all are equal;

These processors share all device resources, and all resources have the same accessibility to the processor, such as: disk, memory, bus, etc.; multiple CPU processors share the same physical memory, and each CPU accesses the same physical address , the time consumed is the same;

Advantages and disadvantages

Advantages: Avoiding structural obstacles, its biggest feature is that all resources are shared;

Disadvantages: SMP-based systems have limited scalability and bottlenecks;

Such as: memory bottleneck limitation, each CPU processor must access the same memory resources through the same bus, if the number of CPUs continues to increase and use the same bus, it will cause memory access conflicts; this will reduce the performance of the CPU;

Practice has proved that the system with SMP architecture can achieve the highest utilization rate when using 2 2 2 ~ 4 4 4 CPUs. If there are more CPUs, the utilization rate will be reduced, wasting the performance of the processor;

The work that the kernel needs to do under the multiprocessor

① Fair sharing: CPU load needs to be shared fairly, and a certain CPU cannot be idle, resulting in waste of resources;

② Process and CPU affinity can be set: affinity can be set for certain types of processes and specified processors, and processes and processors can be matched in a targeted manner;

③ Process migration: The Linux kernel can migrate processes between different CPU processors;

Guess you like

Origin blog.csdn.net/qq_32378713/article/details/128174222