Catching termination of child process and system() call

Geoff Clare gwc at root.co.uk
Tue Feb 19 01:27:43 AEST 1991


In comp.lang.c<9882 at dog.ee.lbl.gov> torek at elf.ee.lbl.gov (Chris Torek) writes:

>(This really belongs in a Unix newsgroup; however, I expect no further
>followups, i.e., I think this will be the decisive answer.)

Sorry to disappoint Chris, but I have something to add to his "decisive
answer".  I have cross-posted to comp.unix.programmer and directed
follow-ups there.  The discussion does have some relevance to 'C' since
it is about the format of the status returned by wait(), and on UNIX
systems this format also applies to the return value of the system()
function.

>The answer, then, is that to wait for a process whose id is `pid' you
>should use:

>	int w, status;

>	if (check_other_wait_results(pid, &status))	/* if necessary */
>	while ((w = wait(&status)) != pid) {
>		if (w == -1 && errno == EINTR)	/* ugly but sometimes... */
>			continue;		/* ...necessary */
>		record_other_wait_result(w, status);	/* if necessary */
>	}

>The exit status of the process, if any, is then `status >> 8' and the
>signal, if any, that caused the process to die is then `status & 0177'.
>The process left a core dump (`image' or `traceback data' to non-Unix
>folks) if `status & 0200' is nonzero.

POSIX does not specify the precise encoding of information in the status
returned by wait(), system(), etc., so portable programs should not
rely on the traditional encoding Chris describes above.  Instead macros
are provided in <sys/wait.h> to extract the relevant data from the status:

     WIFEXITED(status) is non-zero if the child exited normally, in which
case WEXITSTATUS(status) gives the exit code.

     WIFSIGNALED(status) is non-zero if the child was terminated by a signal,
and  WTERMSIG(status) gives the signal number.

     WIFSTOPPED(status) is non-zero if the child was stopped by a signal,
and  WSTOPSIG(status) gives the signal number.
-- 
Geoff Clare <gwc at root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273



More information about the Comp.unix.programmer mailing list