Linux/unix method of assigning process ID and source code implementation

Source: Internet
Author: User

In the Linux/unix system. Each process has a unique process ID that is represented by a nonnegative integer. Though it is the only one. However, the ID of the process can be reused. When a process terminates, its process ID can be used again.

Most Linux/unix systems use deferred reuse algorithms that give the new process ID a different ID than the one used to terminate the process in the near future, primarily to prevent the new process from being mistaken for a terminated previous process that uses the same ID. This article discusses the method of assigning process IDs for Linux/unix and the implementation of source code.

Methods for assigning Process IDs

In most Linux/unix systems, a process ID method is generated by sequentially allocating from 0 to the maximum process ID that can be allocated (different systems). The maximum value is different, for example, some Linux systems are 65536). Once the maximum value is reached, again from a value (different system, this value is not the same, for example, in Mac OS X and HP-UX systems, this value is 100) Start sequentially looking for those who have not yet been used in the ID. There are potential security issues with the method of assigning process IDs here. Because of the ability to obtain information from the system or to extract the content of inter-process communication.

Consider the security issue. Some systems may use other methods to assign process IDs, such as randomly assigning a process ID.

Regardless of the method used to assign the process ID. The system needs to ensure that each process ID is unique.

source code implementation for assigning process IDs on Linux systems

In a Linux system, the range of the kernel-assigned PID is (Reserved_pids, Pid_max_default). In each of the namespace. The PID is successively allocated sequentially (the task in different namespace can have the same ID).

Once the ID reaches the assigned reach limit (in Pseudo-file/proc/sys/kernel/pid_max to see the maximum process ID that can be assigned), find the assignment PID from the beginning. The following is the relevant source code:

struct PID *alloc_pid (struct pid_namespace *ns) {/* omitted some code */for (i = ns->level; i >= 0; i--) {    nr = Alloc_pidmap ( TMP);    if (NR < 0) goto Out_free;    Pid->numbers[i].nr = nr;    PID->NUMBERS[I].NS = tmp;    TMP = Tmp->parent;} /* Omitted some code */}static int alloc_pidmap (struct pid_namespace *pid_ns) {        int i, offset, Max_scan, pid, last = Pid_ns->la St_pid;        struct Pidmap *map;        PID = last + 1;        if (PID >= pid_max)                pid = reserved_pids;        /* and later on ... */        pid_ns->last_pid = pid;        return PID;}

Note that in the Linux kernel, the process PID implementation is not just an int identifier (which, of course, returns to the application, but the PID is a numeric value of type int). The structure of the related implementation can be found in the/include/linux/pid.h. In addition to the ID. It also contains the task list associated with this ID, a reference counter, and a hashed list that can be easily found.

Considerations for Process ID Assignment
1. The PID of the zombie process is temporarily unavailable. It is necessary for the full termination state of its parent process collector to be used, that is, the ability to invoke the wait () function.
2, in detail implementation, the system can randomly assign process PID (of course, is guaranteed not to be used by other processes), so in the application, do not rely on the process PID allocation method.
3. In user space, you may see that the assigned process ID is not contiguous, because the kernel scheduler (scheduling) may have created a process between the application two fork.

In fact. Such a situation is often happening.

References

Advanced programming of the UNIX Environment (second edition)
http://superuser.com/questions/135007/how-are-pids-generated
Http://stackoverflow.com/questions/3446727/how-does-linux-determine-the-next-pid
Http://en.wikipedia.org/wiki/Process_identifier


Linux/unix method of assigning process ID and source code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.