When a child process forked from a parent dies, it still exists to some extent and is left in a zombie state, until it is reaped from a wait() call.
Is is possible to detach this parent-child relationship and have the children be automatically reaped? Maybe orphan off the child to init, or something?
Possible use case: Creating a lot of fire-and-forget processes in a long-lived program, without "holding" on to an increasing list of zombie PIDs that cannot be recycled by the OS.
Per the POSIX _exit() documentation:
In short, to prevent child processes from becoming zombie processes, the simplest way is to call
sigignore( SIGCHLD );
UPDATE
That does impact the ability to wait for any child process, which may not be desired. The
setsid()
library function allows a process to disassociate itself from its parent:The disassociated child process doesn't create a zombie nor send
SIGCHLD
to the parent process on my installed instance of Solaris 11.2.This is an abbreviated daemonization of a "fire-and-forget" child process, only doing what is necessary to prevent creating a zombie or sending
SIGCHLD
to the parent process. For a much fuller daemonization procedure, see Linux daemonizeAs of Python 3.2, you can use subprocess.Popen() and pass start_new_session=True to accomplish this.
The docs state:
https://docs.python.org/3/library/subprocess.html#subprocess.Popen
Note that this does not allow one "to do this from the parent at an arbitrary stage in the child's lifetime" as mentioned in a follow-up comment.