How to start a process with a predefined (in case it is unused) PID?
5,494
It is impossible to start a process with a predefined PID.
If you wanted to do this you'd have to change the implementation of the fork()
(and subsequent) system calls when a new process is spawned, and somehow create a utility that lets you intercept these calls. Also don't forget that the specific implementation of PID allocation depends on the operating system's kernel – it's not the same for OS X and Linux.
See also: How does Linux determine the next PID?
Related videos on Youtube
Author by
static
Updated on September 18, 2022Comments
-
static over 1 year
Is it possible to start/run a process with a predefined PID? In Linux or in OS X (or BSD)?
-
slhck over 10 yearsWhy would you need that?
-
static over 10 yearsto set a monitor for a specific PID before the process started
-
user1686 over 10 yearsThen maybe you're doing the "monitoring" thing wrong...
-
slhck over 10 yearsI was going to say. Maybe you can explain what you really need to do? (And see mywiki.wooledge.org/XyProblem please.)
-
static over 10 years@grawity: why wrong? its a bit more then monitoring (instrumentation). I just want to see what is going on in the process from start on.
-
-
static over 10 yearsis it possible (maybe with any workaround) to use a dtrace/gdb to start a process, pause it immediately after assigning a pid, get it's pid, start a monitor/filter for monitor for that pid and then resume the process?
-
user1686 over 10 years@static: You don't even need a workaround for that – it's basic
fork()
andexec()
usage. Instead of forking a process that immediately execs another program, fork a process that stores its PID and then execs another program. -
static over 10 years@grawity: hm, is it possible to rewrite/customize a fork for an OS X and for a binary program, which process is interesting for me?
-
slhck over 10 years@grawity I think you meant Checkpoint/Restart? I wasn't aware this existed, so thanks for the info. Looked into it a little and it seems it wasn't ever merged, no.
-
mr.spuratic over 10 years@slhck except for
init
of course ;-)