ptg10805159
236 Process Control Chapter 8
Most modern implementations ofexitdo not bother to close the streams. Because the
process is about to exit, the kernel will close all the file descriptors open in the process.
Closing them in the library simply adds overhead without any benefit.
Section 5.6 of McKusick et al. [ 1996 ] contains additional information on the
implementation issues of fork and vfork.Exercises 8.1 and 8.2 continue the
discussion ofvfork.
8.5 exitFunctions
As we described in Section 7.3, a process can terminate normally in five ways:
- Executing areturnfrom themainfunction. As we saw in Section 7.3, this is
equivalent to callingexit. - Calling theexitfunction. This function is defined by ISO C and includes the
calling of all exit handlers that have been registered by callingatexitand
closing all standardI/O streams. Because ISO C does not deal with file
descriptors, multiple processes (parents and children), and job control, the
definition of this function is incomplete for a UNIX system. - Calling the_exitor_Exitfunction. ISOCdefines_Exitto provide a way
for a process to terminate without running exit handlers or signal handlers.
Whether standardI/O streams areflushed depends on the implementation. On
UNIX systems,_Exitand_exitaresynonymous and do not flush standard
I/O streams. The_exitfunction is called byexitand handles the UNIX
system-specific details;_exitis specified by POSIX.1.
In most UNIX system implementations,exit( 3 )is a function in the standardC
library,whereas_exit( 2 )is a system call.
- Executing areturnfrom the start routine of the last thread in the process. The
return value of the thread is not used as the return value of the process,
however.When the last thread returns from its start routine, the process exits
with a termination status of 0. - Calling thepthread_exitfunction from the last thread in the process. As
with the previous case, the exit status of the process in this situation is always 0,
regardless of the argument passed topthread_exit.We’ll say moreabout
pthread_exitin Section 11.5.
The three forms of abnormal termination are as follows:
- Callingabort.This is a special case of the next item, as it generates the
SIGABRTsignal. - When the process receives certain signals. (Wedescribe signals in moredetail in
Chapter 10.) The signal can be generated by the process itself (e.g., by calling
theabortfunction), by some other process, or by the kernel. Examples of