Catching termination of child process and system() call

David W. Lauderback davel at cai.uucp
Thu Jan 24 19:42:30 AEST 1991


In article <YANG.91Jan23133130 at newyork.nff.ncl.omron.co.jp> yang at nff.ncl.omron.co.jp (YANG Liqun) writes:
>
>In article <15745vrm at cathedral.cerc.wvu.wvnet.edu> Vasile R. Montan writes:
>
>> ... I put the following in my main routine:
>> 
>> void dowait()
>>{
>> wait(0)
>
>It should be wait((int *)0).
>
This could be important, but probably isn't the cause of the left around
processes.
>>main()
>> {
>>   ...
>>  signal(SIGCHLD, dowait);
>> ...
>> }
>
>When a child process stopped or exited, SIGCHLD signal will be sent to the
>process and wait system call itself will catch the SIGCHLD signal from a
>child. So you do not need to use 
>signal(SIGCHLD, dowait);
>just use
>   wait(&ret_val)
>in parent process.
If he didn't wait until a signal came in, the parent process would stop until
the child dies.  This is probably not the desired effect.
>
>I think the problem of your code is that a SIGCHLD signal is sent to
>parent process when a child process dies, but the signal is caught and
>then invoke a wait system call which will wait for another SIGCHLD signal.
>
>Yang.
>
Calling wait returns when: a signal occurs OR when a child's status is ready OR
when there is no outstand children.  So the code above should work except for
a timing problem if another signal come in, just as the process calls wait.

However, if you are just trying to get rid of left-over child processes
"zombie processes",
just use:
	signal(SIGCHLD,SIG_IGN);
instead of:
	signal(SIGCHLD, dowait);
and you need no wait. (see signal(2) or signal(3c) in BSD)

FYI: The zombie process is storing the child process' exit status, so must
remain until its parent process has read this information.  SIG_IGN to SIGCHLD
states this process' child's return value should be discarded.
-- 
David W. Lauderback (a.k.a. uunet!cai!davel)
Century Analysis Incorporated
Disclaimer: Any relationship between my opinions and my employer's
	    opinions is purely accidental.



More information about the Comp.lang.c mailing list