Maximum PID in Linux

后端 未结 4 1968
抹茶落季
抹茶落季 2020-12-02 12:38

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 c withou

4条回答
  •  不知归路
    2020-12-02 13:37

    The maximum value of the PID in Linux is configurable. You can access it trough /proc/sys/kernel/pid_max file. 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).

    From the programming perspective, you have to use pid_t type 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:

    $ cat test.cpp 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main ()
    {
        cout << "pid_t max = " << boost::integer_traits::const_max << endl;
    }
    
    $ ./test 
    pid_t max = 2147483647
    

提交回复
热议问题