next up previous Back to Operating Systems Home Page
Next: A4 - Deleting application Up: 1998 term messages Previous: A4 - Socketsreading

A4: Return status of 'Make'

> If we fork a child and call exec to perform the 'make' command, when the
> child terminates and the parent picks up the info using 'wait', how can
> the parent know the exit status of the 'make' command itself?
> 
> For me, if my make fails, the exec still completes successfully, so I'm
> not sure how my parent is supposed to know that it's child's 'make'
> returned an error.

Tsk tsk tsk, are we forgetting what we learned at the beginning
of the course already? ;-)

A succesful exec(2) means that the program you wanted to execute
was found, loaded and started. The exit status of the program itself
is the value returned by its own main() function, or the value it passes
to exit(2). The parent can inspect that value by calling wait(2) or 
waitpid(2). 

All well-behaved UNIX applications are supposed to exit
with an exit status of 0 to indicate successful completion, and 
nonzero to indicate some error. This is the reason why defining
the main() function of a C program as

        void main(void) { ... }

or similar is in general bad practice, even if the compiler swallows
it. If you do this you won't get a warning when you forget to
return 0 at the end, which happens to 99.9% of the novice programmers.
It is not an error, since you may still exit correctly by calling
exit(0) from somewhere in the program, but that's easy to forget too.
So, always define main as

        int main(int argc, char* argv[]) { ... }


As an aside, most shells (including bash, sh, tcsh and csh) store
in the shell variable $? the exit status of the last executed command.
Try
        % echo "All is well and good"; echo $?
        % ls this_file_does_not_exist; echo $?

In a csh(1) (or tcsh(1)) script, you'd test the exit status of a prog with
a construct like thus:

        #!/bin/csh
        proggy          # run proggy
        if ( $? ) then
                echo "Ooops, something went wrong: proggy status = $?"
        endif

The sh(1) or bash(1) equivalent is:

        #!/bin/sh
        proggy          # run proggy
        if [ $? ]; then
                echo "Ooops, something went wrong: proggy status = $?"
        fi


Ciao
Franco


\ Franco Callari