Recognize process numbers in Linux
1. as we all know, a process is an instance of an executable program. But in Linux, a process can be rewritten as an abstract entity defined by the kernel, and allocate various system resources for executing programs to the entity. From the kernel perspective, a process consists of user-space memory and a series of kernel data structures. The user memory space contains the variables used by program code and code, the kernel data structure is used to maintain the Process status information. The information recorded in the kernel data structure includes many process-related IDs) virtual Memory table, open file descriptor table, information about signal transmission and processing, process resource usage and restrictions, current working directory and a large amount of other information. 2. Process descriptor each process has a process ID (PID), which is a positive number used to uniquely identify a process in the system. For various system calls, process numbers can sometimes be used as input parameters, and sometimes as return values. For example, the system calls kill () to allow the caller to send a signal to a process with a specific process number. When you need to create a unique identifier for a process, the process number will come in handy. A common example is to use a process number as a part of the process-related file name (log file name ). In a distributed system, ip: port: start_time: pid can be used to differentiate processes in the entire cluster. This ensures uniqueness and can be quickly located after a problem occurs.
The system calls getpid () to return the process NUMBER of THE calling process. The declaration is as follows: # include <unistd. h> // Always successfully returns process ID of caller pid_t getpid (Void); 3. the Linux kernel restricted process Number of the Process descriptor must be less than or equal to 32767. When a new process is created, the kernel assigns the next available process number to it in order. Whenever the process number reaches the 32767 limit, the kernel resets the process number counter to re-allocate the number from an integer. The allocation method is as follows: once the process number reaches 32767, the kernel resets the process number counter to 300 instead of 1. This is because the low-value process numbers are used by system processes and daemon for a long time. Searching for unused process numbers within this range is only a waste of time. In Linux 2.4 and earlier versions, the maximum number of processes is 32767, defined by the kernel constant PID_MAX. In Linux 2.6, the situation has changed. Although the maximum number of processes is still 32767 by default, you can adjust it through the/proc/sys/kernel/pid_max file specific to the Linux system (value = maximum process + 1 ). On a 32-bit platform, the maximum value of the pid_max file is 32767. However, on a 64-bit platform, the maximum value of the file can be as high as 2 ^ 22 power (about 4 million ), the system can accommodate a large number of processes. 4. The parent process number. Each process has its own parent process. Call getppid () to obtain the process ID of the parent process. The function declaration is as follows: # include <unistd. h> // Always successfully returns ID of parent of caller pid_t getppid (VoidIn fact, the parent process number attribute of each process reflects the tree relationship between all processes on the system. Each parent process has its own parent process, and so on, tracing back to process 1-init process, that is, the ancestor of all processes. Run the pstree command to view the tree relationship. If the parent process of the child process is terminated, the child process will become an orphan, and the init process will then adopt the process () will return process number 1. By viewing the PPid field provided by the/proc/PID/status file specific to the Linux system, you can know the parent process of each process.