[comp.sys.next] <defunct> processes

kraig@milton.acs.washington.edu (Kraig Eno) (05/08/90)

I am trying to make a server process that forks a child whenever a request
comes in.  It works great, except that the children never go away, they
show on a "ps -ax" as <defunct>.  They stay forever, unless I kill and
restart the server.  Anybody have ideas on what that means and how to
properly terminate a child process?

Kraig Eno, kraig@biostr.washington.edu
Structural Informatics Group
Biological Structure, University of Washington

chet@cwns1.CWRU.EDU (Chet Ramey) (05/08/90)

In article <3325@milton.acs.washington.edu> kraig@milton.acs.washington.edu (Kraig Eno) writes:
>I am trying to make a server process that forks a child whenever a request
>comes in.  It works great, except that the children never go away, they
>show on a "ps -ax" as <defunct>.  They stay forever, unless I kill and
>restart the server.  Anybody have ideas on what that means and how to
>properly terminate a child process?

The <defunct> means that the process is a `zombie'.  A zombie process has
for all intents and purposes completed execution, but it hangs around,
occupying a process table slot, until its parent does a `wait' and reaps
it.

Assuming Mach is like 4.3 BSD in this regard (with the addition of void
signal handlers), here is one way to fix it.  Add the following function to
your code:

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

void
reapchild(sig)
int	sig;
{
	union wait st;

	while (wait3(&st, WNOHANG, (struct rusage *) 0) > 0)
		;
}

and add the following statement and declaration to main():

signal(SIGCHLD, reapchild);


When one of your children dies, the server will get SIGCHLD.  `reapchild'
will be called, and will do non-blocking waits until all dead children
have been collected.

Chet Ramey
-- 
Chet Ramey			"I'm majoring in Eastern Philosophy and	
Network Services Group		 cowboy movies -- the yin, the yang, and
Case Western Reserve University	 the bang bang."
chet@ins.CWRU.Edu			-- "American Flyers"