...
To have functionality where the program can quit from within a function registered by at_exit() it is necessary to use a function used for abnormal termination such as _exitExit() or abort().
From the man page of _exit<include _Exit() :
The function _exit terminates the calling process "immediately". Any open file descriptors belonging to the process are closed; any children of the process are inherited by process 1, init, and the process's parent is sent a SIGCHLD signal.description>
| Code Block | ||
|---|---|---|
| ||
#include <stdio.h>
#include <stdlib.h>
#define exitearly 1
void exit1(void)
{
printf("Exit second.\n");
}
void exit2 (void)
{
printf("Exit first.\n");
if (exitearly) {
_exitExit(1);
}
}
int main (void) {
if (expr) {
atexit(exit1);
atexit(exit2);
exit(1);
}
else {
exit2();
}
return 0;
}
|
...