_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()
void _Exit(int status);
Parameters
Parameter | Description |
---|---|
status | Status 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
#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:
- The program prints a starting message.
_Exit()
is called withEXIT_SUCCESS
to terminate the process immediately.- 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
#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:
- An
atexit()
handler namedcleanup()
is registered to be executed on normal termination. - The program prints a starting message.
_Exit()
is called withEXIT_FAILURE
, terminating the process immediately without invoking the cleanup handler.- 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
#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:
- A variable
error_occurred
is set to indicate a critical error. - An error message is printed to the standard error stream.
_Exit()
is invoked withEXIT_FAILURE
to immediately terminate the process.- The termination bypasses any further code execution or cleanup routines.
Program Output:
Critical error encountered. Terminating immediately.