The problem briefly: The following (omitted) 'C' program creates a
daemon process which in turn forks off another child process.
The problem we are experiencing is that under OSF/1 version 3.22 Unix
when the last child exits, its process remains as a <defunct>. This
problem does not occur under AT&T SVR4 Unix.
So far, the replies have told me that I should use the wait3() routine
to wait for the status of the child. I have chosen one, the briefest
and quite clear, to include below. We set up the signal handler so we
don't have to wait for the SIGCLD until we get one. Then it's a quick
routine to accept the signal and the spawning program can get back to
work.
==================
From: joe_at_resptk.bhp.com.au <Joe Spanicek>
I experienced a similar problem on SUN's where a lot of defunct
processes were left hanging after reforking another process. I dont
know whether this will work on OSF/1 but instead of ignoring the
SIGCLD signal you should trap it and use a 'wait3' command so that it
dies off nicely.
That is the parent process doing the forking sets up to trap the
SIGCLD signal from the child process. The parent waits until the child
dies off. If the parent does not wait and continues the child has no
reference to its parent and thus becomes defunct. Thats what I
understand about 'defunct' processes from memory, I could not find any
references on the subject. The following is the code I used to get
around the same problem:
---------------------------------------------------------------------------
/*
* Now fork a process & execute a program
*/
signal(SIGCLD, sig_child); /* trap child's exit */
if ((pid_child = fork()) == 0) { /* child */
execl(.........);
exit(0);
}
-------------------------
/*
* Use WNOHANG in wait3 to stop defuncts
*/
int
sig_child()
{
int pid;
union wait status;
while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0);
return;
}
-------------------------------------------------------------------------------
Thanks also to those others who replied:
alan_at_nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward inodes)
Ray Bellis <Ray.Bellis_at_psy.ox.ac.uk>
"Reinhard Doelz, Biocomputing Basel" <doelz_at_comp.bioz.unibas.ch>
Ilja Hallberg <hallberg_at_elixir.e.kth.se>
--
<< E-mail: inicholl_at_coles.com.au Phone: +61 3 9829 6088, Fax: +61 3 9829 6886 >>
<< Post: Coles Supermarkets, PO Box 480, Glen Iris 3146, Australia >>
Received on Tue May 23 1995 - 03:00:24 NZST