Here is a start (but read below):
static void
child_handler(int sig)
{
pid_t pid;
int status;
/* EEEEXTEERMINAAATE! */
while((pid = waitpid(-1, &status, WNOHANG)) > 0)
;
}
/* Establish handler. */
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, NULL);
Of course, this is all pointless. If the parent simply ignores SIGCHLD
, the children are silently reaped and won't turn into zombies.
Quoting TLPI:
Explicitly setting the disposition of SIGCHLD to SIG_IGN causes any
child process that subsequently terminates to be immediately removed
from the system instead of being converted into a zombie.
So something like this should do the trick for you:
signal(SIGCHLD, SIG_IGN); /* Silently (and portably) reap children. */
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…