What is the difference between abort () and exit ()?
Category: MFC 2011-01-04 14:13 2233 people read reviews (0) favorite reports Unixshellfunctionc language Work
Abort () causes the program to exit abnormally, and exit () can have an incoming value.
---------------------------------------------------------------Abort writes a termination message on stderr (" Abnormal program termination ") and then aborts the program by a call to _exit with exit code 3.
Exit terminates the calling process. Before termination, exit does the following:closes all files writes buffered output (waiting to is output) calls any Regi Stered "Exit Functions" (posted with Atexit)---------------------------------------------------------------exit () You can specify the status of exit, Normal or error. Abort () seems to just exit the program. ---------------------------------------------------------------exit () to execute the Vatexit () setting before exiting the function abort () causes the program to exit abnormally----- ----------------------------------------------------------
Abort () simply quits the program and does nothing. Exit () Do some cleanup work before exiting. ---------------------------------------------------------------in Huazhong University of Science and Technology "C + + programming Practice", the difference is mentioned: according to the object-oriented thinking, the program is also an object, Therefore, the program also has a life and death. C + + After compiling a program, this executes the program: (1) Execute the START function, at this time the program initialization, mainly refers to the global variable initialization. (2) Call the main function. (3) Perform a call-off function to refactor global variables (objects). So the following program will have output, although the main function is empty: #include <stdio.h> int x=printf ("ABCDEF"); void Main () {} Now, let's talk about the difference between abort and exit and return. Return returns, which can be used to deconstruct local variables in main or function, especially if local objects are not refactored, which can cause memory leaks. Exit returns the local variables in the main or function are not destructor, but the function is executed, so the global variables (objects) can be destructor. Abort does not refactor the local variables in main or function, nor does the call-off function, so the global and local objects are not destructor. So, with return more to avoid memory leaks, in C + + with abort and exit are not good habits. Please give points. ---------------------------------------------------------------
Exit () Terminate The calling process after Cleanup (exit) or immediately (_exit). Abort () aborts the current process and returns an error code
____________________________________________________________
#include <stdlib.h>
The exit and _EXIT functions are used to terminate a program normally: _exit immediately into the kernel, exit performs some cleanup (including calls to execute each termination handler, closes all standard I/O streams, etc.) and then enters the kernel. The reason for using a different header file is that exit is indicated by ANSI C, while _exit is described by posix.1. for historical reasons, the Exit function always performs a purge close operation of a standard I/O Library: Call the Fclose function for all open streams. Exit and _exit all take an integer parameter called the terminating state (exit status). Most Unix shells provide a way to check the state of a process's termination. if (a) the function is called without a terminating state, or (b) main executes a re-turn statement with no return value, or (c) main performs an implicit return, the terminating state of the process is defined at the end. This means that the following classical C language programs: #include <stdio.h > main () { printf ("Hello, world/n"); } is incomplete because the main function is not returned with a return statement (implicitly returned), and it does not return a value (terminating state) when it returns to the start routine of C. In addition, if using: &NBSP;&Nbsp; return (0), or exit (0); Returns the terminating state 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) shows main as returning an integer and using exit instead of return, which generates unnecessary warning messages for some C-compilers and Unix lint (1) programs, because these compilers do not understand that exit in main has the same effect as a return statement. The warning message might be "control reaches end of Nonvoid function", which controls the end of non-void functions. One way to avoid this warning message is to use the return statement instead of exit in main. However, the result of this is that you cannot use the UNIX grep utility to find all the exit calls in the program. Another workaround is to describe main as returning void instead of int, and then still calling exit. This also avoids the compiler's warning, but is not correct from a programming perspective. This chapter shows main as returning an integral type, as this is defined by Ansic and posix.1. We will ignore the compiler unnecessary warnings
Actually a lot of people so-calledNormal exit, basically refers to the following two kinds: 1. The specified work must be completed before exiting, such as sending message 2 to a remote port. To take care of those open streams when exiting, such as to properly output buffer data to a file
In this case, the abnormal exit is listed above two points in turn ...
We'll pay attention. Several exit methods of the program: 1. Natural death: Return of the main function 0 2. Brave suicide: Call Exit or Abort 3. Death: The Killer Ctrl+c/kill, Accidentally stepping on a mine (triggering some hardware error), in a stray bullet (uncaught signal, exception)
Hardware error, unable to catch the exception these two situations cannot be controlled, so this is not detailed here. Other program exit methods, can be categorized as abnormal exit, there is nothing more than these two categories: 1. CTRL + C kill and so on, and do not complete the normal exit when the work done 2. Abort exit, in other words, if it is terminated, there is no work to be done, even if killed, it can be seen as a normal exit. Here, let's focus on conditions that meet the "abnormal exit" condition. The main differences between the two exception exits listed above are: 1. The first class is not going to clean up 2 of open file descriptors. The second class cleans up file descriptors, buffers, and abort and exit are the differences between static and global objects, and exit calls the Atexit registration callback function, but the abort and exit are no different if the SIGABRT signal is registered handler.
Finally, give a simple conclusion, do not continue to expand. 1. Complete the cleanup required before exiting by using abort or exit with the callback function
2. Captures common "killers" and executes 1. Common needs to be captured are: SIGINT SIGTERM
-------Signal reference: http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index1.html
Abort is the difference between the Terminate abort exit that will produce the core file ...
-
Abort indicates "abnormal" end to the program, and raises the the POSIX signal SIGABRT, whic H means that's any handler so you had registered for that signal would be invoked and although the program would s Till terminate afterwords in either case. Usually you would use abort with a C program to exit from an unexpected error case where the error is likely to being a bug in the program, rather than something as bad input or a network failure. For example, you might abort if a data structure is found to has a NULL pointer in it when that Shoul D logically never happen.
Exit indicates a "normal" end to the program, although this may still indicate a failure (and not a bug). In other words, you might with an exit error code if the user gave input that could not being parsed, or a file could not being Read. An exit code of 0 indicates success. exitalso optionally calls handlers before it ends the program. These is registered with the atexit and on_exit functions.
std::terminate is, automatically called in a C + + program when there are an unhandled exception. This is essentially the C + + equivalent abort to, assuming that's reporting all your exceptional errors by means of Throwing exceptions. This calls a handler std::set_terminate , which is set by the function, which by default simply calls abort .
Aborb () Program End form