System V and SIGCLD

Karl Kleinpaste karl at osu-eddie.UUCP
Sat May 10 22:59:38 AEST 1986


lindsay at cheviot.newcastle.ac.uk (Lindsay F. Marshall) writes:
>The following code goes into an infinite loop on System V :-
>	trap(sig)
>	int sig;
>	{
>		printf("trapped SIGCLD\n");
>		signal(SIGCLD, trap);	/* reset handler */
>	}
>[followed by main() which forks and then pauses if it's the parent]
>
>The problem is that resetting the SIGCLD trap inside the handler causes the
>signal to be raised again and the handler to be re-entered...... This
>is not documented in the manual page and seems to me to be a bug as if you
>do not reset the handler the system seems to set it to SIG_DFL, meaning that
>you will loose any SIGCLD signals between the handler's exit and your getting
>a chance to call signal again. Anyone have any thoughts, information etc. on
>this problem??

You're almost right, but not quite.  It's not a bug.  The problem your
code demonstrates is an inappropriate way to deal with SIGCLD.  What
you need in the above trap() code is a wait(2) call before the reset
of SIGCLD in signal(2), in order to clean up the zombie child.  SIGCLD
signals queue in SysV - you have to clean up your zombie children _a_s
_t_h_e_y _o_c_c_u_r when you want to use SIGCLD on them.  Be aware that if you
get a SIGCLD for one dead child, call trap() to take care of it, and
then a second child dies while still in trap(), you will immediately
get run through trap() again when signal(2) is called.  And so on for
any <n> zombie children.  This is correctly documented in the manual
page.

I know it works, because I use it heavily in my job-control SysV csh.
-- 
Karl Kleinpaste



More information about the Comp.unix.wizards mailing list