Chapter 26: Monitoring Child Processes

Chapter 26: Monitoring Child Processes
Linux System Programming · wait(), waitpid(), SIGCHLD, Zombies & Orphans
6
Parts
16+
Code Examples
50+
Interview Q&As
TLPI
Chapter 26

Why This Chapter Matters

When a parent process creates children with fork(), it is responsible for monitoring them. Failing to do so causes zombie processes that pile up in the kernel process table and can eventually crash your system. This chapter covers every tool Linux provides for child process monitoring — from basic wait() to asynchronous SIGCHLD-driven reaping.

These concepts appear in almost every Linux interview for embedded, systems, and backend roles.

Topics Covered:

wait() waitpid() waitid() wait3() / wait4() W* macros WIFEXITED WIFSIGNALED WNOHANG WUNTRACED zombie orphan init adoption SIGCHLD SA_NOCLDSTOP SIG_IGN auto-reap sigprocmask sigsuspend race condition

Series Parts

Part 1: The wait() System Call

The fundamental child-reaping call. How it blocks, what it returns, and how to loop over all children. Includes the limitations that led to waitpid().

wait() mechanics ECHILD error Multiple children loop Limitations
Part 2: The waitpid() System Call

Selective waiting, non-blocking polls with WNOHANG, and detecting stopped children with WUNTRACED. The go-to call for most production code.

pid argument WNOHANG WUNTRACED WCONTINUED
Part 3: Wait Status Macros

Decoding the 16-bit status integer using W* macros. How to detect normal exit, signal death, core dumps, stopped state, and continued state.

WIFEXITED WIFSIGNALED WCOREDUMP WIFSTOPPED WIFCONTINUED
Part 4: waitid(), wait3(), and wait4()

Advanced variants — waitid() with siginfo_t and WNOWAIT peek; wait3()/wait4() for resource usage. Includes a full comparison table.

siginfo_t si_code / CLD_EXITED WNOWAIT rusage
Part 5: Orphans and Zombies

What happens when parent or child dies first. Why zombies can’t be killed. Why zombie accumulation is dangerous. How init adopts orphans.

zombie state orphan adoption init (PID 1) process table exhaustion
Part 6: The SIGCHLD Signal

Asynchronous child monitoring. The correct SIGCHLD handler pattern, the signal queuing problem, errno preservation, SA_NOCLDSTOP, SIG_IGN, and the sigprocmask+sigsuspend race-condition fix.

SIGCHLD handler SA_NOCLDSTOP SIG_IGN auto-reap sigsuspend race fix async-signal-safe

Free Linux & Embedded Systems Tutorials

All tutorials on EmbeddedPathashala are completely free. No paywalls, no ads.

Visit EmbeddedPathashala Linux Series Index

Leave a Reply

Your email address will not be published. Required fields are marked *