74

What is the maximum value of the Process ID?

Also, is it possible to change a Process ID?

motoku
  • 218
  • 1
    While it was not possible to change the PID in Linux at the time this question was asked (and the current answers correctly say so), it is now possible to pass a set_tid array to clone3() since Linux 5.5 to accomplish this. – Grisha Levit Aug 10 '21 at 07:08

4 Answers4

92

On Linux, you can find the maximum PID value for your system with this:

$ cat /proc/sys/kernel/pid_max

This value can also be written using the same file, however the value can only be extended up to a theoretical maximum of 32768 (2^15) for 32 bit systems or 4194304 (2^22) for 64 bit:

$ echo 32768 > /proc/sys/kernel/pid_max

It seems to be normative practice on most 64 bit systems to set this value to the same value as found on 32 bit systems, but this is by convention rather than a requirement.

From man 5 proc:

/proc/sys/kernel/pid_max

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. On 32-bit platfroms, 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).

And no, you cannot change the PID of a running process. It gets assigned as a sequential number by the kernel at the time the process starts and that is it's identifier from that time on. The only thing you could do to get a new one is have your code fork a new process and terminate the old one.

SebMa
  • 2,149
Caleb
  • 70,105
  • and we can be sure of kernel bits with this – Aquarius Power Jun 12 '14 at 22:40
  • 3
    A curious semi-counterexample to the "cannot change the PID" story is the case of a multithreaded program, where one of the auxiliary threads does an exec(). It overlays the whole process, but changes the PID (to the TID of the exec'ing thread). – fche Apr 12 '15 at 22:11
  • 4
    The Maximum value of a PID for 32-bit machine is 32767, not 32768. 0 is the scheduler and 1 is init, and then user processes get 2 through 32767. –  Aug 19 '15 at 07:07
  • 3
    user129258 is right, and in agreement with both the manual page and the kernel doco. The maximum allowed value of a PID, as requested by the question, is one less than the value in that pseudo-file. – JdeBP Sep 20 '16 at 09:06
  • @ilkkachu I didn't say it would be 2^32, I said it would be the same value as on 32 bit systems. I updated the wording in this answer to make it more clear what I meant, I hope that helps. – Caleb Jun 26 '18 at 10:47
  • 1
    @user129258, this only holds up if you are using init. There is always the possibility to boot directly to a shell, let's say busybox and do thing manually from there without job control. Or even have some startup scrips and drop back to shell. In any case, the shell will be PID 1 in such a case. – Tim Jun 26 '18 at 11:01
  • @Caleb, ok, I misinterpreted your meaning, then. – ilkkachu Jun 26 '18 at 13:49
  • 1
    Nice, for Ubuntu 20.04 it looks like /proc/sys/kernel/pid_max is now set to 4194304... – Avio Sep 17 '20 at 11:09
  • 1
    @Avio same goes for Debian. – toolforger May 25 '22 at 13:19
  • Twelve years later, this answer is still correct, except for one thing: all modern distros (Ubuntu, Debian, Fedora, etc.) switched the default maximum from 32768 to 4194304. It seems the systemd developers made this decision for everyone, in 2019 (https://github.com/systemd/systemd/commit/0e0d424c0f5e1b8cff32ed51033ee6e2f70a5676). – Nadav Har'El Jun 26 '23 at 07:54
13

Other answers have explained

  • /proc/sys/kernel/pid_max for Linux and
  • 99999 for FreeBSD

But the question didn't specify an operating system. So here are some others:

  • On Solaris, the maximum value of a process ID is a kernel tunable parameter — pidmax in /etc/system — that defaults to 30,000 and that can be set anywhere between 266 and 999,999. Note that this is not max_nprocs, which is a kernel tunable parameter with a subtly different function.
  • On HP-UX 10 the kernel tunables named process_id_min and process_id_max prescribe the range of allowable process IDs.
  • On AIX, process IDs comprise several fields, including a "process slot" and a "generation count" field. The maximum possible value is 0x03FFFFFE, because the fields occupy only the bottom 26 bits of an integer, and bit #0 is always zero except for process #1.
  • On OpenBSD the maximum is 32766.
  • On NetBSD the maximum is 30000.
JdeBP
  • 68,745
7

For Linux

What is the maximum value of the Process ID?

 $ cat /proc/sys/kernel/pid_max
 4194304
 $ sysctl kernel.pid_max
 kernel.pid_max = 4194304

On a 32-bit system, the results would be 32768.

If you are referring to the maximum value that can be achieved

According to the definitions in the Kernel: https://elixir.bootlin.com/linux/latest/source/include/linux/threads.h#L34

/*
 * This controls the default maximum pid allocated to a process
 */
#define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000)

/*

  • A maximum of 4 million PIDs should be enough for a while.
  • [NOTE: PID/TIDs are limited to 2^30 ~= 1 billion, see FUTEX_TID_MASK.]

/ #define PID_MAX_LIMIT (CONFIG_BASE_SMALL ? PAGE_SIZE 8 :
(sizeof(long) > 4 ? 4 * 1024 * 1024 : PID_MAX_DEFAULT))

You can check for the CONFIG_BASE_SMALL in

cat /boot/config-`uname -r`| grep -i config_base_small

In my RHEL system, the calculation limited this to 2^22 = 4 * 1024 * 1024 ~ 4 Million

Test performed

>>> echo 4200000 > /proc/sys/kernel/pid_max; echo $?
bash: echo: write error: Invalid argument
1
>>> echo 4194304 > /proc/sys/kernel/pid_max; echo $?
0
>>> echo 4194305 > /proc/sys/kernel/pid_max; echo $?
bash: echo: write error: Invalid argument
1

Also, is it possible to change a Process ID?

You cannot change the PID of the current process.
For changing the limit though, you can follow below.

## Using PROC interface.
## Changes Temporarily and immediate. It reverts to the default value after reboot 
echo "VALUE" > /proc/sys/kernel/pid_max

Using sysctl interface; It is temporary and immediate too.

sysctl -w kernel.pid_max=VALUE

To persist this value, add the parameter to the either the file /etc/sysctl.conf or some file in /etc/sysctl.d/. To reload it:

sysctl -p [File from which the conf needs to be loaded if present]

Reference:

Stephen Kitt
  • 434,908
DaiCode-1523
  • 171
  • 1
  • 3
4

On FreeBSD the value of PID is between 0 and 99999 according to intro(2) (link). Here's a quote from the manual:

Process ID.
Each active process in the system is uniquely identified by a non-negative integer called a process ID. The range of this ID is from 0 to 99999.

If you want to read the source code on your own then PID_MAX is defined in sys/sys/proc.h (link).