C language _exit () function: End Process Execution
header file:
To define a function:
Function Description: _exit () is used to immediately end the execution of the current process, return the parameter status to the parent process, and close the file that is not closed. This function call does not return, and the SIGCHLD signal is passed to the parent process, which can take the end state of the child process by the wait function.
Additional note: _exit () does not process standard I/O buffers, use exit () if you want to update buffers.
C language On_exit () function: Set the function that is called before the program ends normally
header file:
To define a function:
int On_exit (void (* function) (int void*), void *arg);
Function Description: On_exit () is used to set a function that is called before the program ends normally. When a program calls exit () or returns from Main, the function specified by the parameter function is invoked before the program is actually terminated by exit (). The argument arg pointer passes to the argument function function, see the example.
Return value: Returns 0 if the execution succeeds, otherwise returns-1, the reason for the failure is stored in errno.
Example
#include <stdlib.h>
void my_exit (int status, void *arg)
{
printf ("Before exit ()!\n");
printf ("exit (%d) \ n", status);
printf ("arg =%s\n", (char*) arg);
}
Main ()
{
char * str = ' test ';
On_exit (My_exit, (void *) str);
Exit (1234);
}
Perform:
Before exit ()! Exit (1234) arg = Test
C language Atexit () function: Set the function that is called before the program ends normally
header file:
To define a function:
int atexit (void (*function) (void));
Function Description: Atexit () is used to set a function that is called before the program ends normally. When a program calls exit () or returns from Main, the function specified by the parameter function is invoked before the program is actually terminated by exit ().
Return value: Returns 0 if the execution succeeds, otherwise returns-1, the reason for the failure is stored in errno.
Example
#include <stdlib.h>
void my_exit (void)
{
printf ("Before exit ()!\n");
}
Main ()
{
atexit (my_exit);
Exit (0);
}
Perform: