exit, _exit - terminate a process
#include <stdlib.h> void exit(int status); #include <unistd.h> void _exit(int status);
The exit() function first calls all functions registered by atexit(), in the reverse order of their registration. Each function is called as many times as it was registered.If a function registered by a call to atexit() fails to return, the remaining registered functions are not called and the rest of the exit() processing is not completed. If exit() is called more than once, the effects are undefined.
The exit() function then flushes all output streams, closes all open streams, and removes all files created by tmpfile(). Finally, control is returned to the host environment as described below. The values of status can be EXIT_SUCCESS or EXIT_FAILURE, as described in <stdlib.h>, or any implementation-dependent value, although note that only the range 0 through 255 will be available to a waiting parent process.
The _exit() and exit() functions terminate the calling process with the following consequences:
- All of the file descriptors, directory streams, conversion descriptors and message catalogue descriptors open in the calling process are closed.
- If the parent process of the calling process is executing a wait(), wait3(), waitid() or waitpid(), and has neither 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 will be made available to it when the parent subsequently executes wait(), wait3(), waitid() or waitpid().
- If the parent process of the calling process is not executing a wait(), wait3(), waitid() or waitpid(), and has not set its SA_NOCLDWAIT flag, or set SIGCHLD to SIG_IGN, the calling process is transformed into a zombie process. A zombie process is an inactive process and it will be deleted at some later time when its parent process executes wait(), wait3(), waitid() or waitpid().
- Termination of a process does not directly terminate its children. The sending of a SIGHUP signal as described below indirectly terminates children in some circumstances.
- If the implementation supports the SIGCHLD signal, a SIGCHLD will be sent to the parent process.
- If the parent process has set its SA_NOCLDWAIT flag, or set SIGCHLD to SIG_IGN, the status will be discarded, and the lifetime of the calling process will end immediately. If SA_NOCLDWAIT is set, it is implementation-dependent whether a SIGCHLD signal will be sent to the parent process.
- The parent process ID of all of the calling process' existing child processes and zombie processes is set to the process ID of an implementation-dependent system process. That is, these processes are inherited by a special 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 will be 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, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process in the newly-orphaned process group.
- If the Semaphores option is supported, all open named semaphores in the calling process are closed as if by appropriate calls to sem_close().
- If the Process Memory Locking option is supported, any memory locks established by the process via calls to mlockall() or mlock() are removed. If locked pages in the address space of the calling process are also mapped into the address spaces of other processes and are locked by those processes, the locks established by the other processes will be unaffected by the call by this process to _exit().
- Memory mappings created in the process are unmapped before the process is destroyed.
- If the Message Passing option is supported, all open message queue descriptors in the calling process are closed as if by appropriate calls to mq_close().
- If the Asynchronous Input and Output option is supported any outstanding cancelable asynchronous I/O operations may be canceled. Those asynchronous I/O operations that are not canceled will complete as if the _exit() operation had not yet occurred, but any associated signal notifications will be suppressed. The _exit() operation itself may block awaiting such I/O completion. Whether any I/O is cancelled, and which I/O may be cancelled upon _exit(), is implementation-dependent.
- Threads terminated by a call to _exit() will not invoke their cancellation cleanup handlers or per-thread data destructors.
These functions do not return.
No errors are defined.
None.
Normally applications should use exit() rather than _exit().
None.
atexit(), close(), fclose(), semop(), shmget(), sigaction(), wait(), wait3(), waitid(), waitpid(), <stdlib.h>, <unistd.h>.
Derived from Issue 1 of the SVID.