Process-(3)

Source: Internet
Author: User

1. How the process exits

1) Normal exit
Execute return in the main function
Call the Exit function, do not handle file descriptors, multi-process
Call _exit or _exit.
The last thread of the process executed the return statement
The last thread of the process calls the Pthread_exit function

2) non-normal exit
Call abort to generate a SIGABRT signal
The process receives some signals
The last thread responds to a "cancel" request


3) The difference between exit () and _exit () _exit () Exits:
The essence of _exit () _exit () is the same.
The exit () function allows calling other functions to do some finishing work before exiting, and the _exit () function requires that the process be exited immediately.
Exit () You can call the atexit () registered function before exiting.
Return actually calls the atexit () registered function.

Code Description:

Example 1:exit () function View exit
#include <stdio.h>
#include <stdlib.h>
int main ()
{
printf ("begin\n");
Exit (0);
printf ("end\n");
return 0;
}

[Email protected] day07]# VI exit1.c
[Email protected] day07]# gcc exit1.c-o exit1
[Email protected] day07]#./exit1
Begin

The result above shows that the call to exit () is equivalent to selecting the function of return at this point and exiting the program.

Example 2:exit () function, the effect of calling the Atexit () function
#include <stdio.h>
#include <stdlib.h>
void at ()
{
printf ("At is called\n");
}
int main ()
{
Atexit (at);//Register the function called before exiting
printf ("begin\n");
Exit (0);
printf ("end\n");
return 0;
}


[Email protected] day07]# VI exit2.c
[Email protected] day07]# gcc exit2.c-o exit2
[Email protected] day07]#./exit2
Begin
At is called

The above results indicate:
Execute exit () function, the program exits, you can call a function-atexit () function, on exit, call the function data;


Example 3:_exit () function, forcing exit
#include <stdio.h>
#include <stdlib.h>

void at ()

{
printf ("At is called\n");
}
int main ()

{
Atexit (at);//Register the function called before exiting at
printf ("begin\n");
_exit (0);//exit immediately, do not call at ()
printf ("end\n");
Return 0;//effect is equivalent to exit ()
}
[Email protected] day07]# gcc Exit.c-o exit
[Email protected] day07]#./exit
Begin
The results show that:
_exit () function, is forced to exit, even if there is atexit () function, will not be called, equivalent to the machine has no backup power, direct power off;

Example 4: If you do not use Exit and other functions, only return, how will
#include <stdio.h>
#include <stdlib.h>

void at ()

{
printf ("At is called\n");
}
int main ()
{
Atexit (at);//Register the function called before exiting at
printf ("begin\n");
printf ("end\n");
Return 0;//effect is equivalent to exit ()
}

[Email protected] day07]# VI exit3.c
[Email protected] day07]# gcc exit3.c-o exit3
[Email protected] day07]#./exit3
Begin
End
At is called

Result Description:
The return function, which is equivalent to exit, exits or invokes the Atexit function, that is, the atexit function is hoisted when exiting, but needs to see how to exit, and if the _exit () function exits, it will not be hoisted;

Process-(3)

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.