_exit()

Terminate the program

Synopsis:

#include <stdlib.h>

void _exit( int status );

Arguments:

status
The exit status to use for the program. The value may be zero, EXIT_STATUS, EXIT_FAILURE or any other value. Note that only the least significant bits (i.e. status and 0377) may be available to a waiting parent process.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The _exit() function causes normal program termination to occur.


Note: The functions registered with atexit() aren't called when you use _exit() to terminate a program. If you want those functions to be called, use exit() instead.

The _exit() function does the following when a process terminates for any reason:

  1. Closes all open file descriptors and directory streams in the calling process.
  2. Notifies the parent process of the calling process if the parent called wait() or waitpid(). The low-order 8 bits of status are made available to the parent via wait() or waitpid().
  3. Saves the exit status if the parent process of the calling process isn't executing a wait() or waitpid() function. If the parent calls wait() or waitpid() later, this status is returned immediately.
  4. Sends a SIGHUP signal to the calling process's children; this can indirectly cause the children to exit if they don't handle SIGHUP. Children of a terminated process are assigned a new parent process.
  5. Sends a SIGCHLD signal to the parent process.
  6. Sends a SIGHUP signal to each process in the foreground process group if the calling process is the controlling process for the controlling terminal of that process group.
  7. Disassociates the controlling terminal from the calling process's session if the process is a controlling process, allowing it to be acquired by a new controlling process.
  8. If the process exiting 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 is sent to each process in the newly-orphaned process group.

Returns:

The _exit() function doesn't return.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
    FILE *fp;

    if( argc <= 1 ) {
        fprintf( stderr, "Missing argument\n" );
        exit( EXIT_FAILURE );
    }

    fp = fopen( argv[1], "r" );
    if( fp == NULL ) {
        fprintf( stderr, "Unable to open '%s'\n", argv[1] );
        _exit( EXIT_FAILURE );
    }
    fclose( fp );

    /* 
     At this point, calling _exit() is the same as calling
     return EXIT_SUCCESS;...
    */ 
    _exit( EXIT_SUCCESS );
}

Classification:

POSIX 1003.1

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

abort(), atexit(), close(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), exit(), getenv(), main(), putenv(), sigaction(), signal(), spawn(), spawnl(), spawnle(), spawnlp(), spawnlpe(), spawnp(), spawnv(), spawnve(), spawnvp(), spawnvpe(), system(), wait(), waitpid()