Process status and identifier

The kernel stores all processes in a two-way circular linked list (process linked list). The nodes of the linked list are task_struct, which is called the structure of the process control block. This structure contains all the information related to a process, such as the state of the process, basic information of the process, process identifier, memory-related information, parent process-related information, process-related terminal information, current working directory, and open file information , Received signal information, etc.
The following will elaborate on the two most important fields in the task_struct structure: state (process state) and pid (process identifier).

  1. Process status Processes in
    Linux have the following main statuses.
    (1) Running state (TASK_RUNNING): The process is currently running, or is waiting for scheduling in the running queue.
    (2) Interruptible sleep state (TASK_INTERRUPTIBLE): The process is in a blocked (sleep) state, waiting for certain events to occur or being able to occupy certain resources. A process in this state can be interrupted by a signal. After receiving a signal or being awakened by an explicit wake-up call (such as calling the wake_up series of macros: wake_up, wake_up_interruptible, etc.), the process will transition to the TASK_RUNNING state.
    (3) Uninterruptible sleep state (TASK_UNINTERRUPTIBLE): This process state is similar to the interruptible blocking state (TASK_INTERRUPTIBLE), except that it will not process the signal, and the process that passes the signal to this state cannot change its state. Only when the event it is waiting for occurs, the process is awakened by an explicit wake-up call.
    (4) Stop state (TASK_STOPPED): The execution of the process is suspended. When the process receives SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU and other signals, it will enter the suspended state.
    (5) Zombie state (EXIT_ZOMBIE): The child process ends, the parent process does not exit, and system calls such as the wait function family (such as the waitpid() function) are not used to reclaim the resources of the child process. The child process in this state has given up almost all the memory space, has no 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 its parent process to collect .
    (6) Extinction state (EXIT_DEAD): That is, the process exits, does not occupy any resources, and will not be scheduled, and this state is not visible.
  2. Process Identifier The
    Linux kernel uses a unique process identifier (process ID number) PID (Process ID) to identify each process. PID is stored in the pid field of task_struct.
    When the system is started, the kernel usually acts as a representative of a certain process. A macro current pointing to task_struct is used to record the running process. Current often appears in the kernel code as a pointer to the process descriptor structure. For example, current->pid represents the PID of the process being executed by the processor. When the system needs to view all the processes, the for_each_process() macro is called, which will be much faster than the system searching the array.
    In Linux, the system call functions to obtain the process ID (PID) and parent process ID (PPID) of the current process are getpid() and getppid() respectively.

Guess you like

Origin blog.csdn.net/anton_99/article/details/99702350