Some functions related to normal or abnormal program termination are provided in the C standard library <stdlib.h> , which is described below.
Reference documents:
[1] C and pointers, p298,342
[2] C modern method of programming language (2nd edition), P489
[3] Understanding of the Atexit function and the Exit function
1 abort ()
The abort () function is used to terminate an executing program abnormally. The function prototypes are as follows:
void abort (void)
This function will trigger the SIGABRT signal, you can set a signal processing function for the signal in the program, take any action you want to take before the program terminates (or simply do not terminate), and you can even stop the program.
The abort () function is similar to the exit () function, but calling it causes the program to terminate unexpectedly. The Exit function registered by the atexit () function is not called. Depending on the implementation, it may not clean out the output buffers that contain output data, do not close the open stream, and do not delete the temporary files. The abort () function returns a status code that is defined by the implementation to indicate "unsuccessful termination."
Add:
When the abort () function is called, the SIGABRT signal is actually generated. If there is no function to process the SIGABRT signal, then the program terminates abnormally as described earlier. If the signal handler function is installed (by calling the signal () function) for SIGABRT, the handler function is called. If the processing function returns, then the program terminates abnormally. However, if the processing function does not return (for example, it calls the longjmp () function), then the program does not terminate.
2 atexit ()
The atexit () function can register some functions as exit functions (the Exit Function). The function prototypes are as follows:
int atexit (void (*func) (void))
When you pass a function pointer to the atexit () function, it saves the pointer for future reference. The Exit function is called when the program is about to terminate normally (either by calling exit or because the main function returns). The Exit function cannot accept any arguments .
3 exit ()
The exit () function is used to terminate the program normally. The function prototypes are as follows:
void exit (int)
If the program returns a value ending with the main function, the effect is equivalent to calling the exit () function with this value as a parameter. Executing an exit (n) call anywhere in the program is usually equivalent to executing return n in the main function.
The int parameter is returned to the operating system to prompt the program to complete properly. The predefined symbols exit_success and exit_failure respectively indicate whether the program terminated successfully or failed. The only other portable parameter of the exit () function is 0, which has the same meaning as macro exit_success. Although programs can use other values, their specific meanings will depend on the compiler.
This function is especially useful when a program finds an error condition that makes it impossible to continue execution. You will often call exit () to terminate the program after calling Perrno. Although terminating a program is not the correct way to handle all errors, it is a better practice than failing a program that is doomed to fail.
Note that this function does not return a value. When the exit () function ends, the program has disappeared, so it has nowhere to return.
When the exit () function is called, all functions that are registered as exit functions by the atexit () function are reversed sequentially in the order in which they are registered (parameters are pushed back into the stack, and then advanced out). Then, all the buffers used for the stream are flushed and all open files are closed. Files created with the Tmpfile () function are deleted. The exit status is then returned to the hosting environment, and the program stops executing.
Warning:
Because the program stops executing, the exit () function will never return to its invocation . However, if any function that is registered as an exit function with Atexit () is called exit () again, the effect is undefined. This error can result in an infinite loop, which is likely to terminate only if the stack's memory is exhausted.
4 _exit ()
The _exit () function is similar to the exit () function, but _exit () does not call the Atexit () registered Exit function, nor does it call the signal handler function passed to the signal () function. In addition, the _exit () function does not need to clean the output buffers, close open streams, and delete temporary files, and whether these operations are performed is defined by the implementation. The function prototypes are as follows:
void _exit (int)
According to ISO C, a process can register up to 32 functions, usually these 32 functions are called termination handlers (exit functions), and by calling the Atexit () function to enlist these functions, these functions will be called automatically by the exit () function.
The essential difference between Exi T () and _exit () and _exit () functions is whether to enter the kernel immediately, _exit (), and the _exit () function to enter the kernel immediately after the call, without performing some cleanup, but exit () performs some cleanup. This is why the atexit () function exists because the exit () function needs to perform cleanup and a series of operations, which are actually the actual executions of various so-called cleanup operations. The definition of the Atexit function also gives the programmer a way to use exit to perform some cleanup operations, such as some programs that require additional operations, which can be cleared by this method for specific operations.
The only way the kernel makes the program execute is to call an EXEC () function, and the only way the process voluntarily terminates is by either an explicit or implicit call (via the Exit Function) _exit () or the _exit () function. So the exit function is essentially the encapsulation of the _exit () or _exit () function. Exit executes the custom termination handler first and then executes the I/O library function Cleanup function fclose (), which is why you can continue to apply functions such as printf in the termination handler because the stream object of the I/O library function has not been cleared and can of course continue to be used. After executing all the fclose (), you can perform a true termination function _exit () or _exit () function.
program termination function in C language