at_quick_exit() Function
The at_quick_exit()
function is declared in the header file <stdlib.h>
.
The at_quick_exit()
function allows you to register a function to be executed automatically when quick_exit()
is called. This mechanism provides a way to perform cleanup or final actions during a quick termination of the program. Multiple functions can be registered and they will be executed in reverse order of their registration. It is important to note that the stack of functions registered with at_quick_exit()
is separate from that of atexit()
, though the same function can be used with both.
Syntax of at_quick_exit()
int at_quick_exit(void (*func)(void));
Parameters
Parameter | Description |
---|---|
func | A pointer to a function that takes no arguments and returns no value. This function is executed when quick_exit() is called. |
It is worth mentioning that if multiple functions are registered using at_quick_exit()
, they will be executed in the reverse order of their registration.
Return Value
The function returns zero if the function was successfully registered. A non-zero value indicates a failure in registering the function.
Examples for at_quick_exit()
Example 1: Registering a Single Function for Quick Exit
This example demonstrates how to register a single function to be executed upon calling quick_exit()
.
Program
#include <stdio.h>
#include <stdlib.h>
void quickFunc(void) {
printf("Quick exit function executed.\n");
}
int main(void) {
if (at_quick_exit(quickFunc) != 0) {
printf("Registration of quick exit function failed.\n");
return 1;
}
printf("Calling quick_exit...\n");
quick_exit(0);
return 0; // This line will never be reached.
}
Explanation:
- A function
quickFunc
is defined to print a message. - The function is registered using
at_quick_exit()
. If registration fails, an error message is printed. - The program calls
quick_exit(0)
, which triggers the execution ofquickFunc
.
Program Output:
Calling quick_exit...
Quick exit function executed.
Example 2: Registering Multiple Functions to Demonstrate Reverse Order Execution
This example registers two functions and demonstrates that they are executed in reverse order when quick_exit()
is called.
Program
#include <stdio.h>
#include <stdlib.h>
void firstFunc(void) {
printf("First quick exit function executed.\n");
}
void secondFunc(void) {
printf("Second quick exit function executed.\n");
}
int main(void) {
if (at_quick_exit(firstFunc) != 0 || at_quick_exit(secondFunc) != 0) {
printf("Registration of quick exit functions failed.\n");
return 1;
}
printf("Calling quick_exit...\n");
quick_exit(0);
return 0; // This line will never be reached.
}
Explanation:
- Two functions,
firstFunc
andsecondFunc
, are defined to print distinct messages. - Both functions are registered using
at_quick_exit()
. Registration occurs in the order they are called. - Upon calling
quick_exit(0)
, the registered functions are executed in reverse order, meaningsecondFunc
is executed beforefirstFunc
.
Program Output:
Calling quick_exit...
Second quick exit function executed.
First quick exit function executed.
Example 3: Using at_quick_exit with atexit for Dual Cleanup
This example demonstrates how the same function can be registered with both at_quick_exit()
and atexit()
to perform cleanup actions during both normal and quick exits.
Program
#include <stdio.h>
#include <stdlib.h>
void cleanup(void) {
printf("Cleanup function executed.\n");
}
int main(void) {
if (at_quick_exit(cleanup) != 0) {
printf("Registration with at_quick_exit failed.\n");
return 1;
}
if (atexit(cleanup) != 0) {
printf("Registration with atexit failed.\n");
return 1;
}
printf("Exiting normally using exit(0)...\n");
exit(0);
// Note: Only the atexit function will be called in this case.
}
Explanation:
- A single function
cleanup
is defined to perform cleanup tasks. - The function is registered with both
at_quick_exit()
andatexit()
. - The program calls
exit(0)
, which triggers only the functions registered withatexit()
. (Ifquick_exit()
were used instead, only theat_quick_exit()
functions would be executed.)
Program Output:
Exiting normally using exit(0)...
Cleanup function executed.