Skip to content
Back to posts

Daemons, Orphan Processes, Zombie Processes, and waitpid

An overview of Unix daemon, orphan, and zombie process lifecycles, plus child-process cleanup with wait and waitpid.

A daemon is a long-running background process detached from a controlling terminal. Traditional daemonization forks, creates a new session with setsid, changes working directory and file mode mask, redirects standard streams, and often forks again so the process cannot reacquire a terminal.

An orphan is a running child whose parent has exited. The operating system reparents it to a system process that can eventually collect its exit status. A zombie is different: it has already exited, but its parent has not yet read that status. The kernel retains a small process-table entry until the parent waits for it.

Parents should reap children using wait or waitpid. waitpid(pid, options) can wait for one child, any child, or a process group; WNOHANG makes the call non-blocking. A SIGCHLD handler commonly loops because one signal may correspond to several exited children:

while True:
try:
pid, status = os.waitpid(-1, os.WNOHANG)
if pid == 0:
break
except OSError:
break

Modern service managers usually handle daemon lifecycle, logs, restarts, and file descriptors, so self-daemonization is often unnecessary for new services.


Originally published on SegmentFault.

Copyright notice

Unless otherwise stated, quietin owns the copyright in all articles. All rights reserved. Except as permitted by law or with prior written permission, reproduction or republication, in whole or in part, is prohibited. Permitted quotations must credit the author and link to the original article.