Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site umcp-cs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: exit() from child and status in parent wait() Message-ID: <5049@umcp-cs.UUCP> Date: Sat, 20-Apr-85 03:44:14 EST Article-I.D.: umcp-cs.5049 Posted: Sat Apr 20 03:44:14 1985 Date-Received: Mon, 22-Apr-85 02:18:56 EST References: <2530@drutx.UUCP> Organization: U of Maryland, Computer Science Dept., College Park, MD Lines: 61 This probably belongs in net.unix, but . . . Anyway, the reason you are getting strange exit stati is that your code is wrong. "wait" wants a pointer, not a value. If you are running 4BSD, there is a nice header file (in different places in 4.1 and 4.2, grr) that defines exactly what wait() fills in; for other systems, you just have to use an "int *" and pick out the fields yourself. The following code forks and picks up the return status, handing errors that might crop up too: #include #include /* in 4.1 */ f() { register int pid, w; union wait status; /* wait gives us back one of these, */ /* but can use "int" if you have to */ if ((pid = fork()) < 0) { /* probably out of processes */ /* try to do something about it here */ return (-1); } if (pid == 0) { /* child */ /* do exec()y stuff here */ _exit(16); /* exit() flushes stdio buffers, which can print stuff twice */ /* NOTREACHED */ } while ((w = wait (&status)) != pid && w > 0) ; if (w <= 0) { /* catastrophe? */ /* probably a program bug */ /* gripe, perhaps, here */ return (-1); } /* * Now have status.w_retcode, which is exit code; * status.w_termsig, which is signal (if any) * that terminated proc; and status.w_coredump, * which is true iff the process left us a core * file. */ /* * Alternatively, "status" can be an integer, in * which case the low 7 bits are the signal, the * eight bit the core-dump flag, and the next eight * bits the exit code. */ /* do something here based on exit code */ } Note that if you are implementing a server which will be running as root, the only reason fork() would fail is if the proc table is full, so forks should be retried if necessary and practical. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251) UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@maryland