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?

Share:
5,494

Related videos on Youtube

static
Author by

static

Updated on September 18, 2022

Comments

  • static
    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
      slhck over 10 years
      Why would you need that?
    • static
      static over 10 years
      to set a monitor for a specific PID before the process started
    • user1686
      user1686 over 10 years
      Then maybe you're doing the "monitoring" thing wrong...
    • slhck
      slhck over 10 years
      I was going to say. Maybe you can explain what you really need to do? (And see mywiki.wooledge.org/XyProblem please.)
    • static
      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
    static over 10 years
    is 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
    user1686 over 10 years
    @static: You don't even need a workaround for that – it's basic fork() and exec() usage. Instead of forking a process that immediately execs another program, fork a process that stores its PID and then execs another program.
  • static
    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
    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
    mr.spuratic over 10 years
    @slhck except for init of course ;-)