Subject: Re: Zombie processes
To: Jeremy C. Reed <reed@reedmedia.net>
From: Todd Vierling <tv@wasabisystems.com>
List: netbsd-help
Date: 04/17/2001 16:00:58
On Tue, 17 Apr 2001, Jeremy C. Reed wrote:

: What if you don't want to wait() for it? For example, I have a little X
: app that when I click in it, it forks a new process and then execve()'s a
: browser (using the URL from the X copy buffer). (It creates a lot of
: zombies!) I want to wait() for it, but I don't want my parent application
: to stall until the child closes -- any code examples?

Install a signal handler on SIGCHLD such as the following:

#include <sys/types.h>
#include <sys/wait.h>

...

void scan_for_zombies(void) {
	int status;

	while (wait4(-1, &status, WNOHANG, NULL) > 0);
}

Note that because the wait4 call has first argument -1 and last argument
NULL, this can be reduced to your choice of waitpid(-1, &status, WNOHANG) or
wait3(&status, WNOHANG, NULL) to interoperate the source code with other
systems which lack wait4().

-- 
-- Todd Vierling <tv@wasabisystems.com>  *  Wasabi NetBSD:  Run with it.
-- NetBSD 1.5 now available on CD-ROM  --  http://www.wasabisystems.com/