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

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

问题描述

Linux 如何确定将用于进程的下一个 PID?这个问题的目的是为了更好地理解Linux内核.不要害怕发布内核源代码.如果 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 print(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 apache 我得到了相当多的进程:

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 如何选择下一个数字?前几个 PID 仍在运行,以及最近打印的 PID.apache 如何选择重用这些 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.

一些相关代码:

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 在用户空间中没有出现顺序的原因是因为内核调度可能会在您的进程的 fork() 调用之间分叉一个进程.事实上,这很常见.

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天全站免登陆