3

I'm wondering what is the meaning of the process identifier in Linux, is it the order of the process?

Is it a code that identifies the nature of the process or simply a number randomly generated to uniquely identify a process?

Are different processes with a similar PID related in some way?

2 Answers 2

9

When the Linux kernel launches a new process it assigns a unique integer number to it starting with 1 sequentially. You may notice that /sbin/init is normally PID 1 because it's the first launched process.

Are different processes with a similar PID related in some way?

No. If their PIDs are close they might have been launched approximately at the same time. On a 32bit Linux PIDs are limited to 32768, so once the kernel reaches this number, it will start again. On a 64bit Linux PIDs are limited to 2^22 and rarely overflow (unless you have a very long uptime and launch and stop thousands of processes).

6
  • I haven’t checked, but I think the limit can be raised on 32-bit kernels too. Jan 19 at 12:44
  • I started a process and the system wrote PID 26471 on the bash, then I started another process and the system wrote 4582. How it is possible that it passed from 26471 to the end and again to 4582 if PIDs are assigned consecutively? Jan 19 at 15:00
  • The counter might have overflown and started from 1. Would you mind editing your post and adding ps axc output? It's safe to do. Jan 19 at 15:12
  • 2
    The PID range is determined by /proc/sys/kernel/pid_max. While this may be absurdly large for a 64-bit system, most distributions that I've checked still limit this to 32768.
    – doneal24
    Jan 19 at 20:20
  • 3
    @MégasAléxandros: Practically all OSs have been randomizing PIDs for decades, because of the obvious security implications. Jan 19 at 22:16
5

PID is short for ‘process identifier’. That’s exactly what it is, a way to ‘uniquely’ identify a process on the system. Note that I have ‘uniquely’ in quotes here. This is because a PID is only unique for the lifetime of the process it is assigned to.

As far as how a PID is chosen, it varies by system. The original approach is to simply assign the next number that has not been used, up to some maximum value, and once you get to that max you start reusing previously used but currently unused numbers, starting back from the lowest such number again.

Linux takes that original approach, because it’s simple and fast. The downside is that some poorly written software may rely on PIDs in ways that it should not (such as using them to seed an internal random number generator or create a temporary file name), which allows for some potentially nasty local exploits if you’re using such software (but such software is thankfully increasingly rare).

Some systems, such as OpenBSD, instead pick PIDs at random from the currently unused values between 1 and the maximum. This eliminates the local security issues, but in exchange it slows down creation of new processes, opens you up to random users on the internet potentially nasty things (such as the exploit outlined in this security Stack Exchange question), and possibly breaks software that expects PIDs to not be reused quickly.

Others, like FreeBSD, allow you to choose either approach, or alternatively use a middle ground. This allows you to pick which particular set of security issues you want to deal with (hint, it’s probably the local issues, not the remote issues), or even choose a middle ground (which is usually the correct choice).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.