Xref: utzoo comp.unix.programmer:1130 comp.lang.c:36309 Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!ukc!pyrltd!root44!gwc From: gwc@root.co.uk (Geoff Clare) Newsgroups: comp.unix.programmer,comp.lang.c Subject: Re: Catching termination of child process and system() call Message-ID: <2608@root44.co.uk> Date: 18 Feb 91 14:27:43 GMT References: <1991Jan24.023750.19569@tkou02.enet.dec.com> <14965@smoke.brl.mil> <1991Jan25.022950.10683@tkou02.enet.dec.com> <14977@smoke.brl.mil> <1356@geovision.UUCP> <9882@dog.ee.lbl.gov> Followup-To: comp.unix.programmer Organization: UniSoft Ltd., London, England Lines: 45 In comp.lang.c<9882@dog.ee.lbl.gov> torek@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 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 (Dumb American mailers: ...!uunet!root.co.uk!gwc) UniSoft Limited, London, England. Tel: +44 71 729 3773 Fax: +44 71 729 3273