Process termination methods

Source: Internet
Author: User
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);}


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.