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.
- The parent sets up a signal handler for SIGUSR1 (say), so that it can know when the child is ready.
- The parent forks, then waits for SIGUSR1.
- The child sets up a signal handler for SIGUSR2, so that it can know when the parent is ready.
- The child sends SIGUSR1 to its parent to signal readiness, then waits for SIGUSR2.
- The parent does whatever it wants to do with the now-running child process (whose PID it now knows).
- The parent sends SIGUSR2 to the child.
- The child
exec
s into the process that it actually wanted to start all along. - 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.)