_exit() — End a process and bypass the cleanup
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1
XPG4 XPG4.2 Single UNIX Specification, Version 3 |
both |
Format
#define _POSIX_SOURCE
#include <unistd.h>
void _exit(int status);
General description
Ends the current process and makes an exit status value for the process available to the system.
The argument status specifies
a return status for the process that is ending. Ending the process
has the following results:
- _exit() closes all open file descriptors and directory streams in the caller.
- If the caller's parent is currently suspended because of wait() or waitpid(), the low-order 8 bits of status become available to the parent. For a discussion on accessing those 8 bits, refer to waitpid() — Wait for a specific child process to end.
- If the caller's parent is not currently suspended because of wait() or waitpid(), _exit() saves the status value so that it can be returned to the parent if the parent calls wait() or waitpid().
- A
SIGCHILD
signal is sent to the parent process. - If the process calling _exit() is a controlling process, the
SIGHUP
signal is sent to each process in the foreground process group of the controlling terminal belonging to the caller. - If the process calling _exit() is a controlling process, _exit() disassociates the associated controlling terminal from the session. A new controlling process can then acquire the terminal.
- Exiting from a process does not end its child processes directly.
The
SIGHUP
signal may end children in some cases. Children that survive when a process ends are assigned a new parent process ID. The new parent process ID is always 1, indicating the root ancestor of all processes. - If a process ends and orphans a process group and if a member
of that group is stopped, each member of the group is sent a
SIGHUP
signal, followed by aSIGCONT
signal. - All threads are ended, and their resources cleaned up. (Threads are MVS™ tasks that call a z/OS® UNIX callable service.) POSIX-level thread cleanup routines are not executed. These include cleanup routines created with pthread_cleanup_push() and destructor routines created with pthread_key_create().
These results occur whenever a process ends. _exit()
does not cause C runtime library cleanup to be performed; therefore,
stream buffers are not necessarily flushed.
Note: If _exit() is issued
from a TSO/E address space, it ends the calling task and all its subtasks.
Special behavior for C++: If _exit() is called in a C++ program, the program terminates without leaving the current block, and destructors are not called for local (automatic) variables. In addition, unlike exit(), destructors for global (static) variables are not called.
Returned value
_exit() is always successful and returns no values.
No value is stored in errno for this function.
Example
CELEBE05
/* CELEBE05
This example ends a process.
*/
#define _POSIX_SOURCE
#include <unistd.h>
#include <stdio.h>
main() {
puts("Remember that stream buffers are not automatically");
puts("flushed before _exit()!");
fflush(NULL);
_exit(0);
}
Output
Remember that stream buffers are not automatically
flushed before _exit()!
Related information
- stdlib.h — Standard library functions
- unistd.h — Implementation-specific functions
- abort() — Stop a program
- atexit() — Register program termination function
- close() — Close a file
- exit() — End program
- _Exit() — Terminate a process
- fork() — Create a new process
- sigaction() — Examine or change a signal action
- signal() — Handle interrupts
- wait() — Wait for a child process to end