[Reprinted] the abort function of the exit termination function of the program

Source: Internet
Author: User
Tags exit in
The exit and _ exit functions are used to terminate a program normally: _ Exit immediately enters the kernel, and exit is used to execute some cleanup operations (including calling and executing various termination processes, close all standard I/O streams, etc.) and then enter the kernel. The reason for using different header files is that exit is described by ansi c, while _ exit is described by posix.1.
For historical reasons, the exit function always executes the clear and close operation of a standard I/O Library: calls the fclose function for all open streams. Both exit and _ exit contain an integer parameter, which is called Exit status ). Most Unix shells provide methods to check the termination status of a process. If (a) if these functions are called without termination, or (B) Main executes a re turn statement without return values, or (c) Main executes an implicit return, the termination status of the process is defined at the end. This means that the following Classic C language programs:
# Include <stdio. h>
Main ()
{
Printf ("Hello, world \ n ");
}
Is incomplete, because the main function does not use the return statement to return (implicit return), it does not return a value (Termination status) when returning to the C startup routine ). In addition, if you use:
Return (0); or
Exit (0 );
Returns the termination status 0 to the process that executes the program (often a shell process. In addition, the description of the main function should actually be:
Int main (void)
The description of main is to return an integer and Use Exit instead of return, which generates unnecessary warning information for some C compilation programs and Unix LINT (1) programs, because these compilation programs do not understand that the exit and return statements in main serve the same purpose. The warning message may be "control reaches end of nonvoid function (control to the end of non-void function )". One way to avoid this warning is to use the return statement rather than exit in main. However, the result is that the Unix grep Utility cannot be used to find all exit calls in the program. Another solution is to describe main as return void instead of int, and then still call exit. This also avoids the warning of compiling programs, but it is not correct from the perspective of program design. This chapter marks main as an integer, because this is defined by ansic and posix.1. We will ignore warnings that are unnecessary to compile the program.
Atexit Function
According to ansi c, a process can register up to 32 functions, which will be automatically called by exit. We call these functions exit handler and use the atexit function to register these functions.
# Include <stdlib. h>
Int atexit (void * (func) (void ));
Return Value: 0 if successful, and 0 if an error occurs.
The atexit parameter is a function address. When you call this function, you do not need to send any parameters to it or expect it to return a value. Exit calls these functions in reverse order. If the same function is registered multiple times, it is also called multiple times.
The mechanism for terminating the processing program was recently introduced by ansi c. S v R 4 and 4. 3 + B S D both provide this mechanism. Earlier versions of System V and 4. 3 B s d do not provide this mechanism.
According to ansi c and posix.1, exit first calls each termination handler, and then calls fclose multiple times as needed to close all open streams. The figure shows how a C program is started and how it is terminated. Note that the only way to run the program in the kernel is to call an e x e c function. The only way to voluntarily terminate a process is to call _ e x I t explicitly or implicitly (call e x I t. A process can also be ended by a signal involuntarily.

There are three methods for normal Process Termination and two methods for abnormal Process Termination.
(1) normal termination:
(A) execute the r e t u r n statement in the m a I n function. As described in section 7. 3, this is equivalent to calling e x I t.
(B) Call the e x I t function. This function is defined by ansi c. Its operations include calling various termination handlers (terminating a handler logging on when calling a t e x I t function ), disable all standard I/O streams. Because ansi c does not process file descriptors, multi-process (parent, sub-process), and job control, this definition is incomplete for the u n I x system.
(C) Call the _ e x I t system call function. This function is called by e x I t, which processes the details Of u n I x. _ E x I t is described by p o s I X. 1.
(2) termination of exceptions:
(A) call a B o r t. It generates the S I g a B RT signal, so it is a special case of Abnormal Termination.
(B) When a process receives a signal. (Chapter 1 0 describes the signal in greater detail .) Processes themselves (such as calling the B o r t function), other processes and kernels can generate signals sent to a process. For example, if a process accesses a storage unit out of its address space or is divided by 0, the kernel generates a signal for the process. No matter how the process is terminated, the same piece of code in the kernel will be executed. This code disables all
Open the descriptor and release the memory used by it. For any of the above Termination cases, we all want the termination process to notify its parent process how it is terminated. For e x I t and _ e x I t, this is achieved by the exit status parameter passed to them. In case of abnormal termination, the kernel (not the process itself) generates a termination status that indicates the cause of the exceptional termination ). In any case, the parent process of the terminating process can use the w a I t or w a I t p I d function (described in the next section) to obtain its termination state.
Note that "Exit status" is used here (it is a parameter passed to e x I t or _ e x I t, or the return value of m a I n) and "Terminate"
Status. When _ e x I t is finally called, the kernel switches its exit status to termination status.

/////*********************************** *************************

 

Abstract: This article describes how to use several error handling functions, such as abort, exit, atexit, and strerror, and provides specific sample programs.


Function Name: Abort
Function: terminates a process abnormally.
Usage: void abort (void );
Header file: # include <stdlib. h>
Note: The abort function is a serious function. When it is called, it will cause the program to terminate abnormally,
Instead of clearing the memory, such as releasing the memory.
Program example:
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Puts ("about to abort... \ n ");
Abort ();

Puts ("This will never be executed! \ N ");
Exit (exit_success );
} [Root @ localhost error_process] # GCC abort. c
[Root @ localhost error_process] #./A. Out
About to abort ....

Abandoned

Header file # include <stdlib. h>
Define the function void exit (INT status );

Exit () is used to terminate the execution of the current process normally and return the parameter status to the parent process,
All the buffer data of the process is automatically written back and closed.
It does not exit without any cleanup as abort does, but does not exit the program after all cleanup tasks are completed.

Atexit (set the function called before the program ends normally)

Header file # include <stdlib. h>
Define the function int atexit (void (* function) (void ));

Atexit () is used to set a function called before the program ends normally. When the program passes
When exit () is used or is returned from main, the function specified by the function parameter is first
And then exit.

If the returned value is successful, 0 is returned. Otherwise,-1 is returned. The cause of failure is stored in errno.

# Include <stdlib. h>
# Include <stdio. h>

Void my_exit (void)
{
Printf ("Before exit... \ n ");
}

Int main (void)
{
Atexit (my_exit );
Return 0;
} [Root @ localhost error_process] # GCC atexit. c
[Root @ localhost error_process] #./A. Out
Before exit ....

Strerror (return the description string of the error cause)

Header file # include <string. h>
Define the function char * strerror (INT errnum );
Strerror () is used to query the description string of the error cause based on the error code of the errnum parameter, and then return the string pointer.
In this case, if you pass a strerror to errno, you can get a readable prompt, instead of a cold number.

Return the string pointer that describes the cause of the error.

# Include <string. h>
# Include <stdio. h>

Int main (void)
{
Int I;

For (I = 0; I <10; I ++)
{
Printf ("% d: % s \ n", I, strerror (I ));
}

Return 0;
}

[Root @ localhost error_process] # GCC strerror. c
[Root @ localhost error_process] #./A. Out
0: Success
1: operation not permitted
2: no such file or directory
3: no such process
4: interrupted system call
5: Input/Output Error
6: no such device or address
7: argument list too long
8: exec format Error
9: Bad file descriptor
[Root @ localhost error_process] #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.