Is a process PID guaranteed to stay the same as long as the process doesn't die?

12,234

Solution 1

Yes, it's guaranteed. Process will have the same PID even if its image will be replaced with another one by exec system call.

Solution 2

Well...mostly.

As other posters have said any particular process will retain it's PID indefinitely. Even through one or more instances of exec.

However, a programmer who wished to evade that guarantee on a unix box would have no trouble at all. He would simple fork, ignore HUP signals in the daughter and then kill the mother process. The result of which would be the daughter carrying on with a new PID and give the appearance that the process changed it's PID.

Solution 3

PID in Linux and Windows are unique to that process. PIDs will never change.

Share:
12,234

Related videos on Youtube

Reiik
Author by

Reiik

Updated on September 18, 2022

Comments

  • Reiik
    Reiik over 1 year

    I'm doing some simulation work with Linux, and the code I'm working on right now only works under the assumption that, once a process has been assigned a PID by the kernel, that PID will remain the same until the process is killed. I would assume this is true but since I'm kind of new to the Linux world I just want to see if there are any special circumstances I ought to be aware of. Thanks!

  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 12 years
    Well, a malicious programmer can break the guarantee, but this is generally true of well behaved programs.
  • xaizek
    xaizek almost 12 years
    I can't agree with you, since fork creates new process with same image it's naturally that this new process has another PID value. In case from your answer I would say that program changes its PID (by changing its process), but not the process (and the question is about processes).
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 12 years
    That is, of course, strictly true, but that simply reflects the definition of "process" that the OS uses. The user will see a continuity of state with a changing number, and if she was counting on the presence/absence of the PID to determine if some state was present on the machines this would be a problem. That's why I called the behavior malicious.
  • Martin Thoma
    Martin Thoma over 6 years
    Just to clarify: If I write the process and I don't want the PID to change, it will not change. There is basically no chance I accidentally do something that changes the PID.
  • Martin Thoma
    Martin Thoma over 6 years
    Do you have a source for the claim that the PID doesn't change?
  • Martin Thoma
    Martin Thoma over 6 years
    Do you have a source for the claim that the PID doesn't change?
  • Martin Thoma
    Martin Thoma over 6 years
    Do you have a source for the claim that the PID doesn't change?
  • xaizek
    xaizek over 6 years
    @MartinThoma, all man credentials says about PID is that it's assigned on process creation and is preserved across execve(). Since PID is assigned only on process creation, persistence of PID follows.