NAME
exit, atexit, _exit - Terminates a process
LIBRARY
Standard C Library (libc.a): atexit(), _exit()
SYNOPSIS
#include <stdlib.h>
void exit (
int status );
void _exit (
int status );
int atexit (
void (*function) ( void ) );
PARAMETERS
status Indicates the status of the process.
function Points to a function that is called at normal pro-
cess termination for cleanup processing. A push-
down stack of functions is kept, such that the
last function registered is the first function
called. Any function which is registered more
than once will be repeated. Up to 32 functions
can be specified with atexit().
DESCRIPTION
The atexit() function registers functions to be called at
normal process termination for cleanup processing.
The exit() function terminates the calling process after
calling the Standard I/O Library _cleanup() function to
flush any buffered output. Then it calls any functions
registered previously for the process by the atexit() func-
tion, in the reverse order to that in which they were
registered. In addition, the exit() function flushes all
open output streams, closes all open streams, and removes
all files created by the tmpfile() function. Finally, it
calls the _exit() function, which completes process termina-
tion and does not return.
The _exit() function terminates the calling process and
causes the following to occur:
o All of the file descriptors, directory streams, and
message catalog descriptors open in the calling process
are closed. Since the exit() function terminates the
process, any errors encountered during these close
operations go unreported.
o Terminating a process by exiting does not terminate its
child processes. Instead, the parent process ID of all
of the calling process child processes and zombie child
processes is set to the process ID of init. The init
process thus inherits each of these processes, catches
the SIGCHLD signals they generate, and calls the wait()
function for each of them.
o If the parent process of the calling process is running
a wait() or waitpid() function, it is notified of the
termination of the calling process and the low-order 8
bits (that is, bits 0377 or 0xFF) of the status parame-
ter are made available to it.
o If the parent process is not running a wait() or
waitpid() function when the child process terminates,
it may do so later on, and the child's status will be
returned to it at that time. Meanwhile, the child pro-
cess is transformed into a zombie process, and its
parent process is sent a SIGCHLD signal to notify it of
the termination of a child process.
A zombie process is a process that occupies a slot in
the process table, but has no other space allocated to
it either in user or kernel space. The process table
slot that it occupies is partially overlaid with time
accounting information to be used by the times() func-
tion. (See the sys/proc.h header file.)
A process remains a zombie until its parent issues one
of the wait functions. At this time, the zombie is laid
to rest, and its process table entry is released.
o The parent process is sent a SIGCHLD signal when a
child terminates; however, since the default action for
this signal is to ignore it, the signal usually is not
seen.
If an exiting child's parent is ignoring the SIGCHLD
signal, the child's parent process ID is changed to
that of the initialization process, init, which will
catch the SIGCHLD signal and call the wait() function.
o If the process is a controlling process, a SIGHUP sig-
nal is sent to each process in the foreground process
group of the controlling terminal belonging to the cal-
ling process. The controlling terminal is disassoci-
ated from the session, allowing it to be acquired by a
new controlling process.
o If the exit of a process causes a process group to
become orphaned, and if any member of the newly
orphaned process group is stopped, then a SIGHUP signal
is sent to each newly orphaned process.
o Each attached shared memory segment is detached and the
value of shm_nattach in the data structure associated
with its shared memory identifier is decremented by 1.
o For each semaphore for which the calling process has
set a semadj value, that semadj value is added to the
semval of the specified semaphore. (The semop() func-
tion provides information about semaphore operations.)
o If the process has a process lock, text lock, or data
lock, an unlock is performed. (See the plock() func-
tion.)
o An accounting record is written on the accounting file
if the system accounting routine is enabled. (The
acct() function provides information about enabling
accounting routines.)
o Locks set by the fcntl(), flock(),and lockf() functions
are removed.
If a thread calls the _exit() function, the entire process
exits and all threads within the process are terminated.
NOTES
The system init process is used to assist cleanup of ter-
minating processes. If the code for the init process is
replaced, the program must be prepared to accept SIGCHLD
signals and issue a wait() function for each.
AES Support Level: Full use
RETURN VALUES
The exit() function and _exit() function do not return. The
atexit() function returns 0 (zero) if successful, and a
nonzero value if there has been an attempt to register more
exit() functions than can be held in the atexit() array.
RELATED INFORMATION
Functions: acct(2), sigaction(2), times(3), wait(2),
sigvec(2)
Acknowledgement and Disclaimer