Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to start/run a process with a predefined PID? In Linux or in OS X (or BSD)?

share|improve this question
    
Why would you need that? –  slhck Aug 15 '13 at 18:35
    
to set a monitor for a specific PID before the process started –  static Aug 15 '13 at 18:45
1  
Then maybe you're doing the "monitoring" thing wrong... –  grawity Aug 15 '13 at 19:23
    
I was going to say. Maybe you can explain what you really need to do? (And see mywiki.wooledge.org/XyProblem please.) –  slhck Aug 15 '13 at 19:25
    
@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 Aug 15 '13 at 23:50

1 Answer 1

up vote 1 down vote accepted

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|improve this answer
    
Odd, I could have sworn the Linux clone() syscall had a custom PID flag from the "checkpoint/resume" patches. But I guess those weren't merged. –  grawity Aug 15 '13 at 19:28
    
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? –  static Aug 15 '13 at 19:29
    
@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. –  grawity Aug 15 '13 at 19:31
    
@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? –  static Aug 15 '13 at 19:32
1  
@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. –  slhck Aug 15 '13 at 19:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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