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!
3 Answers
Yes, it's guaranteed. Process will have the same PID even if its image will be replaced with another one by exec system call.
-
Well, a malicious programmer can break the guarantee, but this is generally true of well behaved programs. Aug 5, 2012 at 1:08
-
1I 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).– xaizekAug 5, 2012 at 8:16 -
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. Aug 6, 2012 at 17:22
-
1
-
2@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.– xaizekSep 5, 2017 at 13:05
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.
-
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. Sep 5, 2017 at 8:31
-
1
PID in Linux and Windows are unique to that process. PIDs will never change.
-
1