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 represented by a nonnegative integer. Although it is unique, 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 that was recently terminated, primarily to prevent the new process from being mistaken for a terminated previous process that uses the same ID. This paper discusses the method of Linux/unix allocation process ID and the source code implementation.

Methods for assigning Process IDs

In most Linux/unix systems, a process ID method is generated by sequentially allocating from 0, up to the maximum process ID that can be allocated (different systems, the maximum value is not the same, for example some Linux systems are 65536). Once the maximum value is reached, re-starting from a value (different system, this value is not the same, such as in Mac OS X and HP-UX systems, this value is 100) to 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 you can get information from the system or extract the content of interprocess communication. Given security concerns, some systems may use other methods to assign process IDs, such as randomly assigning a process ID. Regardless of how the process ID is assigned, the system needs to ensure that each process ID is unique.

Implementation of the source code for allocating process IDs on Linux systems

In a Linux system, the kernel assigns the PID to the range (Reserved_pids, Pid_max_default), in each namespace, the PID is successively allocated sequentially (in different namespace the task can have the same ID). Once the ID reaches the assigned reach limit (in Pseudo-file/proc/sys/kernel/pid_max you can see the maximum process ID that can be assigned), find the assignment pid from scratch. 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, PID is just 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 includes a task list associated with this ID, a reference counter, and a hashed list that can be conveniently located.

Process ID assignment Things to be aware of
1. The PID of the zombie process is temporarily unavailable and requires all the terminating state of its parent process collector to be used, that is, the wait () function needs to be called before it can be used.
2, the 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 application two fork. In fact, this happens often.

Resources

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


Related Article

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.