exit, atexit, unatexit, _exit, 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;
void _Exit (Status)
int Status;
#include <sys/limits.h>
int atexit ( Function)
void (*Function) (void);

int unatexit (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 is functionally equivalent to the _exit subroutine. The _Exit subroutine does not call functions registered with atexit or any registered signal handlers. The way the subroutine is implemented determines whether open streams are flushed or closed, and whether temporary files are removed. The calling process is terminated with the consequences described below.

  • All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process are closed.
  • If the parent process of the calling process is executing a wait or waitpid, and has not set its SA_NOCLDWAIT flag nor set SIGCHLD to SIG_IGN, it is notified of the calling process' termination and the low order eight bits (that is, bits 0377) of status are made available to it. If the parent is not waiting, the child's status is made available to it when the parent subsequently executes wait or waitpid.
  • If the parent process of the calling process is not executing a wait or waitpid, and has neither set its SA_NOCLDWAIT flag nor set SIGCHLD to SIG_IGN, the calling process is transformed into a zombie process. A zombie process is an inactive process that is deleted at some later time when its parent process executes wait or waitpid.
  • Termination of a process does not directly terminate its children. The sending of a SIGHUP signal indirectly terminates children in some circumstances. This can be accomplished in one of two ways. If the implementation supports the SIGCHLD signal, a SIGCHLD is sent to the parent process. If the parent process has set its SA_NOCLDWAIT flag, or set SIGCHLD to SIG_IGN, the status is discarded, and the lifetime of the calling process ends immediately. If SA_NOCLDWAIT is set, it is implementation defined whether a SIGCHLD signal is sent to the parent process.
  • The parent process ID of all of the calling process' existing child processes and zombie processes are set to the process ID of an implementation defined system process.
  • Each attached shared memory segment is detached and the value of shm_nattch (see shmget) in the data structure associated with its shared memory ID is decremented by 1.
  • For each semaphore for which the calling process has set a semadj value (see semop), that value is added to the semval of the specified semaphore.
  • 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 is sent to each process in the newly orphaned process group.
  • All open named semaphores in the calling process are closed as if by appropriate calls to sem_close.
  • Memory mappings that were created in the process are unmapped before the process is destroyed.
  • Any blocks of typed memory that were mapped in the calling process are unmapped, as if the munmap subroutine was implicitly called to unmap them.
  • All open message queue descriptors in the calling process are closed.
  • Any outstanding cancelable asynchronous I/O operations may be canceled. Those asynchronous I/O operations that are not canceled complete as if the _Exit subroutine had not yet occurred, but any associated signal notifications are suppressed.

    The _Exit subroutine may block awaiting such I/O completion. The implementation defines whether any I/O is canceled, and which I/O may be canceled upon _Exit.

  • Threads terminated by a call to _Exit do not invoke their cancelation cleanup handlers or per thread data destructors.
  • If the calling process is a trace controller process, any trace streams that were created by the calling process are shut down.

The unatexit subroutine is used to unregister functions that are previously registered by the atexit subroutine. If the referenced function is found, it is removed from the list of functions that are called at normal program termination.

Parameters

Item Description
Status Indicates the status of the process. May be set to 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits are available to a waiting parent 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.

The unatexit() subroutine returns a value of 0 if the function referenced by Function is found and removed from the atexit list. Otherwise, a nonzero value is returned.