[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]
Base Operating System and Extensions Technical Reference, Volume 1
exit, atexit, or _exit Subroutine
Purpose
Terminates a process.
Library
Standard C Library (libc.a)
Syntax
#include <stdlib.h>
void exit (Status)
int Status;
void _exit (Status)
int Status;
#include <sys/limits.h>
int atexit (Function)
void (*Function) (void);
Description
The exit subroutine terminates the calling process after calling the standard I/O library _cleanup function to flush any buffered output. Also, it calls any functions registered previously for the process by the atexit subroutine. The atexit subroutine registers functions called at normal process termination for cleanup processing.
Normal termination occurs as a result of either a call to the exit subroutine or a return statement in the main function.
Each function a call to the atexit subroutine registers must return. This action ensures that all registered functions are called.
Finally, the exit subroutine calls the _exit subroutine, which completes process termination and does not return. The _exit subroutine terminates the calling process and causes the following to occur:
- The _exit subroutine attempts to cancel outstanding asynchronous I/O requests by this process. If the asynchronous I/O requests cannot be canceled, the application is blocked until the requests have completed.
- All of the file descriptors open in the calling process are closed. If Network File System (NFS) is installed on your system, some of these files can be remote. Because the _exit subroutine terminates the process, any errors encountered during these close operations go unreported.
- If the parent process of the calling process is running a wait call, 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 parameter are made available to it.
- If the parent process is not running a wait call when the child process terminates, it may still do so later on, and the child's status is returned to it at that time.
- The parent process is sent a SIGCHLD signal when a child process terminates; however, since the default action for this signal is to ignore it, the signal is usually not seen.
- Terminating a process by exiting does not terminate its child processes.
- Each attached shared memory segment is detached and the shm_nattch value in the data structure associated with its shared memory identifier is decremented by 1.
- 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 subroutine provides information about semaphore operations.)
- If the process has a process lock, text lock, or data lock, an unlock routine is performed. (See the plock subroutine.)
- An accounting record is written on the accounting file if the system accounting routine is enabled. (The acct subroutine provides information about enabling accounting routines.)
- Locks set by the fcntl, lockf, and flock subroutines are removed.
- If the parent process of the calling process is not ignoring a SIGCHLD signal, the calling process is transformed into a zombie process, and its parent process is sent a SIGCHLD signal to notify it of the end of a child process.
- A zombie process 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 subroutine. (See the sys/proc.h file.)
- A process remains a zombie until its parent issues one of the wait subroutines. At this time, the zombie is laid to rest (deleted), and its process table entry is released.
- Terminating a process 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 inherits each of these processes, and catches their SIGCHLD signals and calls the wait subroutine for each of them.
- If the process is a controlling process, the SIGHUP signal is sent to each process in the foreground process group of the controlling terminal belonging to the calling process.
- If the process is a controlling process, the controlling terminal associated with the session is disassociated from the session, allowing it to be acquired by a new controlling process.
- If the exit of the process causes a process group to become orphaned, and if any member of the newly orphaned process group is stopped, a SIGHUP signal followed by a SIGCONT signal will be sent to each process in the newly orphaned process group.
Note: The system init process is used to assist cleanup of terminating processes. If the code for the init process is replaced, the program must be prepared to accept SIGCHLD signals and issue a wait call for each.
Parameters
Status |
Indicates the status of the process. |
Function |
Specifies a function to be called at normal process termination for cleanup processing. You may specify a number of functions to the limit set by the ATEXIT_MAX function, which is defined in the sys/limits.h file. A pushdown stack of functions is kept so that the last function registered is the first function called. |
Return Values
Upon successful completion, the atexit subroutine returns a value of 0. Otherwise, a nonzero value is returned. The exit and _exit subroutines do not return a value.
Implementation Specifics
These subroutines are part of Base Operating System (BOS) Runtime.
Related Information
The acct subroutine,
lockfx,
lockf, or
flock subroutines,
sigaction,
sigvec, or
signal subroutine,
times subroutine,
wait,
waitpid, or
wait3 subroutine.
Asynchronous I/O Overview in AIX Version 4.3 Kernel Extensions and Device Support Programming Concepts.
[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]