_Exit() Function

The _Exit() function is declared in the header file <stdlib.h>.

The _Exit() function terminates the calling process immediately by returning control to the host environment. Unlike exit(), it bypasses regular cleanup tasks such as calling destructors, flushing streams, or executing functions registered with atexit() or at_quick_exit(), making it useful in scenarios where immediate termination is required without any cleanup overhead.


Syntax of _Exit()

</>
Copy
void _Exit(int status);

Parameters

ParameterDescription
statusStatus code indicating the termination state. A value of 0 or EXIT_SUCCESS signals success, EXIT_FAILURE signals failure, and other values depend on system-specific behavior.

It is important to note that using _Exit() bypasses cleanup operations like flushing I/O buffers or calling functions registered with atexit(). This function should be used only when such cleanup is not necessary or desired.


Return Value

The _Exit() function does not return as it terminates the process immediately.


Examples for _Exit()

Example 1: Immediate Termination with Successful Status

This example demonstrates the basic usage of _Exit() to terminate a program with a success status, bypassing normal cleanup operations.

Program

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

int main() {
    printf("Program starting...\n");
    _Exit(EXIT_SUCCESS);
    /* This line will never be executed */
    printf("This message will not be printed.\n");
    return 0;
}

Explanation:

  1. The program prints a starting message.
  2. _Exit() is called with EXIT_SUCCESS to terminate the process immediately.
  3. Any code following the _Exit() call is not executed.

Program Output:

Program starting...

Example 2: Termination Without Calling atexit Handlers

This example compares the behavior of _Exit() with exit() by registering an atexit() handler. Notice that the atexit() handler is not invoked when using _Exit().

Program

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

void cleanup(void) {
    printf("Cleanup handler executed.\n");
}

int main() {
    atexit(cleanup);
    printf("Starting process...\n");
    _Exit(EXIT_FAILURE);
    /* The following line is never executed and the cleanup handler is not called */
    printf("This will not print.\n");
    return 0;
}

Explanation:

  1. An atexit() handler named cleanup() is registered to be executed on normal termination.
  2. The program prints a starting message.
  3. _Exit() is called with EXIT_FAILURE, terminating the process immediately without invoking the cleanup handler.
  4. Thus, the cleanup message is not printed.

Program Output:

Starting process...

Example 3: Using _Exit() in Error Handling

This example demonstrates using _Exit() in an error handling scenario, where immediate termination is required due to a critical error, without performing any cleanup.

Program

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

int main() {
    int error_occurred = 1;

    if (error_occurred) {
        fprintf(stderr, "Critical error encountered. Terminating immediately.\n");
        _Exit(EXIT_FAILURE);
    }

    /* Code beyond this point is never reached */
    printf("This will not execute.\n");
    return 0;
}

Explanation:

  1. A variable error_occurred is set to indicate a critical error.
  2. An error message is printed to the standard error stream.
  3. _Exit() is invoked with EXIT_FAILURE to immediately terminate the process.
  4. The termination bypasses any further code execution or cleanup routines.

Program Output:

Critical error encountered. Terminating immediately.