Overview & #160; & #160; & #160; exit normally & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; return [return] & #160; & #160; & #160; & #; from the main function; & #160; & #160; & #160; & #160; & #160; & #1 overview
Exit normally
Returns [return] from the main function.
CALL exit
Call _ exit
Exit unexpectedly
Call abort to generate a SIGABOUT signal
End by signal Ctrl + c [SIGINT]
Test [exit/_ exit]
// Try to view the print output of the program # include
# Include
# Include
Using namespace std; int main () {cout <"In main, pid =" <getpid (); // remove endl; // principle: associate with the terminal, stdout is a row buffer. in a file, it is a full buffer. // For details, refer to the relevant section of Advanced Programming in UNIX environment // exit (0); it is a C library function, explanation: _ exit (0 );}
As shown in the figure, the system call _ exit directly falls into the kernel, while the C language library function is through a series of system cleanup work, and then calls the Linux kernel
Int main () {cout <"In main, pid =" <getpid (); fflush (stdout); // added the buffer refresh work_exit (0 );}
Summary differences between exit and _ exit
1) _ exit is a system call, and exit is a c-library function.
2) exit will clear the I/O cache
3) exit will execute a call to terminate the processing program // terminate the processing program as follows
Terminate the processing program: atexit
#include
int atexit(void (*function)(void));
Test procedure:
Void exitHandler1 (void) {cout <"If exit with exit, the function exitHandler will be called1" <endl;} void exitHandler2 (void) {cout <"If exit with exit, the function exitHandler will be called2" <endl;} int main () {cout <"In main, pid = "<getpid () <endl; atexit (exitHandler1); // note that after registration, execute atexit (exitHandler2); exit (0 );}
Exceptional termination
void exitHandler1(void){ cout << "If exit with exit, the function exitHandler will be called1" << endl;}void exitHandler2(void){ cout << "If exit with exit, the function exitHandler will be called2" << endl;} int main(){ cout << "In main, pid = " << getpid() << endl; atexit(exitHandler1); atexit(exitHandler2); abort(); //exit(0);}