Linux如何确定下一个PID? [英] How does Linux determine the next PID?

查看:199
本文介绍了Linux如何确定下一个PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux如何确定下一个PID它将使用一个过程?这个问题的目的,是为了更好地理解Linux内核。不要害怕张贴内核源代码code。如果PID号的顺序分配Linux如何填补空白?当它击中到底会发生什么?

How does Linux determine the next PID it will use for a process? The purpose of this question is to better understand the Linux kernel. Don't be afraid to post kernel source code. If PIDs are allocated sequentially how does Linux fill in the gaps? What happens when it hits the end?

例如,如果我运行Apache的PHP脚本,做了< PHP打印(getmypid());?> 相同的PID会被打印出来因为虽然命中刷新几分钟。这段时间是多少请求Apache是​​接收的功能。即使只有一个客户端将PID最终将改变。

For example if I run a PHP script from Apache that does a <?php print(getmypid());?> the same PID will be printed out for a few minutes while hit refresh. This period of time is a function of how many requests apache is receiving. Even if there is only one client the PID will eventually change.

当PID变化,这将是一场势均力敌的数字,但怎么关闭?数量似乎没有完全连续的。如果我做了的ps aux | grep的阿帕奇我得到一个公平的多个进程:

When the PID changes, it will be a close number, but how close? The number does not appear to be entirely sequential. If I do a ps aux | grep apache I get a fair number of processes:

Linux如何选择这下一个号码?的previous少数的PID的仍在运行,以及已打印的最近的PID。如何阿帕奇选择重用这些PID的?

How does Linux choose this next number? The previous few PID's are still running, as well as the most recent PID that was printed. How does apache choose to reuse these PIDs?

推荐答案

的内核(RESERVED_PIDS,PID_MAX_DEFAULT)的范围内分配的PID。但是这样做的每个命名空间顺序(在不同的命名空间中的任务可以有相同的ID)。万一范围耗尽时,PID分配环绕。

The kernel allocates PIDs in the range of (RESERVED_PIDS, PID_MAX_DEFAULT). It does so sequentially in each namespace (tasks in different namespaces can have the same IDs). In case the range is exhausted, pid assignment wraps around.

一些相关的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;
}

alloc_pidmap()

static int alloc_pidmap(struct pid_namespace *pid_ns)
{
        int i, offset, max_scan, pid, last = pid_ns->last_pid;
        struct pidmap *map;

        pid = last + 1;
        if (pid >= pid_max)
                pid = RESERVED_PIDS;
        /* and later on... */
        pid_ns->last_pid = pid;
        return pid;
}

请注意,在内核的上下文的PID不仅仅是 INT 标识符的更多;相关结构在 /include/linux/pid.h 找到。除了ID,它包含了ID,一个引用计数器,以便快速访问一个哈希表节点的任务列表。

Do note that PIDs in the context of the kernel are more than just int identifiers; the relevant structure can be found in /include/linux/pid.h. Besides the id, it contains a list of tasks with that id, a reference counter and a hashed list node for fast access.

原因的PID在用户空间未出现顺序是因为内核调度可能会在餐桌你的进程叉()电话之间的过程。这是很常见的,其实。

The reason for PIDs not appearing sequential in user space is because kernel scheduling might fork a process in between your process' fork() calls. It's very common, in fact.

这篇关于Linux如何确定下一个PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆