Add Book to My BookshelfPurchase This Book Online

Chapter 11 - Processes

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Program Termination
As mentioned above, the operating system saves the termination status of a process that terminates. The termination status can be retrieved later by the parent process (we will describe how to do this later in the chapter). The termination status contains information about whether the process terminated normally or abnormally, and, if it terminated abnormally, the reason for termination.
When a process terminates normally, it may optionally return an exit status to the parent process. The exit status is a small integer value that can communicate information about how things went. Convention dictates that a zero exit status be used to indicate that everything went fine, no errors occurred. A non-zero exit status usually indicates that something went wrong, although this is not always the case. It is up to the programmer to define the meanings for non-zero exit status values. Many programs simply use exit status 1 to indicate something went wrong, without being more specific (error messages usually supplement this). But some programs have several different exit status values, with special meaning assigned to each one. For example, the grep utility exits with status 0 if matches were found, status 1 if no matches were found, and status 2 if the pattern specification was erroneous. For an example of even more special meanings, look at the manual page for the fsck program.
A program provides an exit status to the parent process by using the exit function:
    #include <stdlib.h>
    void exit(int status);
The status argument is the exit status. The function sets the exit status, and then causes the program to terminate.
The exit function is a library routine, defined by ANSI C, that closes all the Standard I/O Library streams the process has open, and then calls another function, _exit. The _exit function does a number of things, including closing all the process' open files, sending a SIGCHLD signal to the parent process, setting the process' child processes' parent process IDs to 1, freeing up any interprocess communication resources used by the process, and so forth. The reason that these chores are not performed by exit itself is that ANSI C does not specify operating system-dependent functionality, and thus cannot specify everything exit should do.
The exit function exists in all versions of UNIX. UNIX implementations that support ANSI C also allow the programmer to register up to 32 functions to be called automatically at the time the program exits, either by calling exit or by returning from main. These functions are registered by using the atexit function:
    #include <stdlib.h>
    int atexit(void (*func)(void));
Each function registered is called, with no arguments, when the program exits. The functions are called in the reverse order of their registration. Again, this functionality is only available in ANSI C.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback