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 without reading /proc/sys/kernel/pid_max
by hand?
Is there a library?

- 8,990
- 23
- 27

- 7,454
- 12
- 57
- 64
-
AFAIK, PID_MAX in Linux is UINT_MAX, which depends on the cpu – Geoffrey Jun 09 '11 at 14:11
-
`UINT_MAX` does not depend on the CPU in Linux. It's always 0x7fffffff. – R.. GitHub STOP HELPING ICE Jun 09 '11 at 14:23
-
Erm, I meant `INT_MAX`. `pid_t` is of course a signed int, not unsigned, but I mentally copied what gnif wrote. :-) – R.. GitHub STOP HELPING ICE Jun 09 '11 at 14:29
-
`PID_MAX_LIMIT` is defined in `threads.h`. See https://elixir.bootlin.com/linux/latest/source/include/linux/threads.h#L33. – Sony Santos Jan 10 '19 at 06:46
4 Answers
It's 32768 by default, you can read the value on your system in /proc/sys/kernel/pid_max
.
And you can set the value higher on 64-bit systems (up to 222 = 4,194,304) with:
echo 4194304 > /proc/sys/kernel/pid_max
Read more here:
http://www.cs.wisc.edu/condor/condorg/linux_scalability.html (via archive.org)
-
1Ok that was not what I wanted to do but I ended up reading the value from the file. I guess there is no other way. – Alexander Stolz Jun 15 '11 at 09:57
-
3The Condor link in your answer went 404, unfortunately. I've replaced it with a link through archive.org, of course it'd be better if you know of a current version of the document. – derobert Nov 18 '13 at 18:04
-
Is the limit shown in that `pid_max` file inclusive? Can `getpid()` return that very number or is the maximum that number minus one? (i.e. by default, can a process have PID 32768 or is the maximum 32767?) – Alexis Wilke Sep 14 '18 at 21:01
-
2Nice, for `Ubuntu 20.04` it looks like `/proc/sys/kernel/pid_max` is now set to `4194304`... – Avio Sep 17 '20 at 11:10
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 <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

- 44,533
- 10
- 148
- 133
-
9Question is tagged C, not C++. The second half of your answer does not apply to C and is not possible in C. – R.. GitHub STOP HELPING ICE Jun 09 '11 at 14:22
-
4@R: This is just an example, if I knew how to get compile-time traits for pid_t in C, I'd write C. I am sure there is some equivalent with macros. – Jun 09 '11 at 14:24
-
-
@bfontaine you are right, the first one is wrong, (pid_t)-1 should work though and is computed at compile time. – yuyichao Apr 14 '14 at 01:14
-
@yuyichao `pid_t` is signed, and functions like `getpid` return -1 to indicate an error. The maximum value can be found with a macro as described in the question http://stackoverflow.com/questions/2053843/min-and-max-value-of-data-type-in-c - for example `(pid_t)((1LLU<<((sizeof(pid_t)*8)-1))-1LLU)` is `2147483647` on my machine. – craig65535 Feb 05 '15 at 08:11
-
1@bfontaine it would be more like `pow(2, 8 * sizeof(pid_t))`. (not a perfect solution, but I wanted to clear up the glaring error with raising 8 to the number bytes, which is barely even related to the desired value) – MickLH Jan 22 '16 at 18:15
-
1
-
not only does it not work in C, in C++ it gives the wrong answer. (too high) – Jasen Mar 28 '22 at 22:37
From the proc(5)
man page:
/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_max
can be set to any value up to 2^22 (PID_MAX_LIMIT
, approximately 4 million).

- 1
- 1

- 132,704
- 33
- 254
- 328
It seems that Ubuntu 20.04 has pushed the limit to the maximum (4194304):
% cat /proc/sys/kernel/pid_max
4194304

- 129,958
- 22
- 279
- 321
-
-
1@MaasoudAsadi interesting, I guess it might be because WSL2 might have another kernel... – Antti Haapala -- Слава Україні Jul 27 '20 at 16:59
-
1