Starting a suspended process

(This is not fancy and is probably common knowledge, but I only learned how to do it today.)

Say you want to start a process, then log its PID, then allow it to run (so that if it starts logging to the console, you’re guaranteed to have done all your logging before it starts polluting stdout).

This is easy on Windows: CreateProcess accepts CREATE_SUSPENDED. But on Linux, it’s not obvious.

  1. The parent sets up a signal handler for SIGUSR1 (say), so that it can know when the child is ready.
  2. The parent forks, then waits for SIGUSR1.
  3. The child sets up a signal handler for SIGUSR2, so that it can know when the parent is ready.
  4. The child sends SIGUSR1 to its parent to signal readiness, then waits for SIGUSR2.
  5. The parent does whatever it wants to do with the now-running child process (whose PID it now knows).
  6. The parent sends SIGUSR2 to the child.
  7. The child execs into the process that it actually wanted to start all along.
  8. The parent unregisters the SIGUSR1 handler.

Note that forked processes have the same signal handlers as the parent, but those signal handlers are blatted by an exec.

(Good lord is this not investment advice. It’s hard to express just how much of a novice I am at signal handling.)