Linux 中的最大 PID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6294133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:04:08  来源:igfitidea点击:

Maximum PID in Linux

clinuxpid

提问by Alexander Stolz

I am porting an application from Tru64 to Linux and it uses PID_MAX defined in limits.h. Linux doesn't have that define. How do I find PID_MAX in cwithout reading /proc/sys/kernel/pid_maxby hand? Is there a library?

我正在将一个应用程序从 Tru64 移植到 Linux,它使用在limits.h 中定义的PID_MAX。Linux 没有这个定义。如何在不手动阅读的情况下在c 中找到 PID_MAX /proc/sys/kernel/pid_max?有图书馆吗?

采纳答案by Exos

It's 32768 by default, you can read the value on your system in /proc/sys/kernel/pid_max.

默认情况下它是 32768,您可以在/proc/sys/kernel/pid_max.

And you can set the value higher on 64-bit systems (up to 222= 4,194,304) with:

您可以在 64 位系统上设置更高的值(最多 2 22= 4,194,304):

echo 4194304 > /proc/sys/kernel/pid_max

Read more here:

在此处阅读更多信息:

http://www.cs.wisc.edu/condor/condorg/linux_scalability.html (via archive.org)

http://www.cs.wisc.edu/condor/condorg/linux_scalability.html(通过archive.org)

回答by Exos

The maximum value of the PID in Linux is configurable. You can access it trough /proc/sys/kernel/pid_maxfile. This file (new in Linux 2.5) specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). The default value for this file, 32768, results in the same range of PIDs as on earlier kernels. The value in this file can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million).

Linux 中 PID 的最大值是可配置的。您可以通过/proc/sys/kernel/pid_max文件访问它。该文件(Linux 2.5 中的新文件)指定了 PID 环绕的值(即,该文件中的值比最大 PID 大 1)。此文件的默认值 32768 产生与早期内核相同的 PID 范围。此文件中的值可以设置为最多 2^22(PID_MAX_LIMIT,大约 400 万)的任何值。

From the programming perspective, you have to use pid_ttype to work with process ID. You can even access its min/max values using integer traits. Here is an example of doing that using C++ and Boost on Linux 2.6.X running on x86_64 platform:

从编程的角度来看,您必须使用pid_t类型来处理进程 ID。您甚至可以使用整数特征访问其最小值/最大值。以下是在 x86_64 平台上运行的 Linux 2.6.X 上使用 C++ 和 Boost 执行此操作的示例:

$ cat test.cpp 
#include <sys/types.h>
#include <iostream>
#include <boost/integer_traits.hpp>

using namespace std;

int main ()
{
    cout << "pid_t max = " << boost::integer_traits<pid_t>::const_max << endl;
}

$ ./test 
pid_t max = 2147483647

回答by Jonathon Reinhart

From the proc(5)man page:

proc(5)手册页:

/proc/sys/kernel/pid_max(since Linux 2.5.34)

This file specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). PIDs greater than this value are not allocated; thus, the value in this file also acts as a system-wide limit on the total number of processes and threads. The default value for this file, 32768, results in the same range of PIDs as on earlier kernels. On 32-bit platforms, 32768 is the maximum value for pid_max. On 64-bit systems, pid_maxcan be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million).

/proc/sys/kernel/pid_max(从 Linux 2.5.34 开始)

此文件指定 PID 环绕的值(即,此文件中的值比最大 PID 大 1)。不分配大于此值的 PID;因此,此文件中的值还充当系统范围内进程和线程总数的限制。此文件的默认值 32768 产生与早期内核相同的 PID 范围。在 32 位平台上,32768 是 pid_max 的最大值。在 64 位系统上,pid_max可以设置为最多 2^22(PID_MAX_LIMIT大约 400 万)的任何值。