Re: fork problems (zombie children)

J. Sean Connell (
Thu, 9 May 1996 00:17:05 +1200 (NZST)

On Mon, 6 May 1996, Thomas Zerucha wrote:

> What am I doing wrong? The following program:
> #include <signal.h>
> #include <sys/wait.h>
> main(){
> for(;;) {
> if( !fork() ) {
> exit(0); /* child, exit immediately */
> }
> sleep(1);
> }
> }
> when run with PID=8704 creates children that stay undead until the parent
> process terminates, e.g. from "ps -x":
> I ran out of virtual memory on my 32Mb machine on a different app (lynx)
> after lots of these were created.
you're never wait()ing for the child processes. they'll sit around in the
zombie state until you do.

(when a process is in the zombie state all its resources are freed (incl.
memory etc.), but the process itself stays around till its parent wait()s
to get the exit status. even if you don't care about it, you have to do
it, unless you do signal(SIGCHLD, SIG_IGN) (or SIGCLD on a SysVish

J. Sean Connell               Systems Software Analyst, ICONZ         "Oh life is a glorious cycle of song,            a medley of extemporanea,
#include <stddisc.h>           And love is a thing that can never go wrong...
                               And I'm Queen Marie of Romania."
I *hate* Sun Type 4 kbs!         --Dorothy Parker