exit() from child and status in parent wait()

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Sat Apr 20 11:34:49 AEST 1985


> if((pid = fork()) == NULL) {
Failure is indicated by -1 not by NULL (which is 0).
>    perror();
perror() requires a (char *) argument to be printed.

>       execl("cmd","cmd","arg",0);
The last argument should be (char *)0.
>       perror();
(See previous perror note; also, exec failure is most often
handled in the parent branch of the fork.)
>       exit(16);
No!  exit() flushes the stdio buffers, but does not affect
the same buffered data in the parent branch.  Use _exit(127)
instead.  (_exit() is like exit() without any cleanup actions.)

>       while((wid = wait((unsigned int *)rtn_code)) != pid);
Here is the main problem; you need to pass the ADDRESS of
rtn_code to wait().  This is &rtn_code, not what you have
written (which asks for the CONTENTS of rtn_code to be
interpreted as an address).  As a matter of style, I suggest
also that the null statement ; be written on a separate line.

>       if(rtn_code == 16) {
ANY nonzero rtn_code should be treated as unsuccessful
execution of the child.  The low 8 bits of rtn_code are
a general class of termination (0 for normal exit) and
the next lowest 8 bits are the returned exit status code
(for normal exit; they encode other things for other
classes of termination).  Only a normal exit with 0 exit
status code should be considered successful.



More information about the Comp.lang.c mailing list