The difference between Linux exit and _exit

Source: Internet
Author: User

Today, we look at the difference between exit and _exit, and actually exit calls the _exit Exit function, except that exit does some extra work before calling _exit, which is why the exit is not as close to the underlying system call as it is. , it should be said that the standard C library functions have been packaged. _exit included in the header file Unistd.h, exit is included in the header file Stdlib.h, let's take a look at their function prototypes.

Man _exit:

Obviously, the _exit function does these three things:

1. Let the calling process terminate immediately.

2. Close all file descriptors opened by this process.

3. All child processes of the calling process are initialized by the INIT process adoption, and the calling process will send sigchld to his parent process (all because he is about to quit and, of course, to settle his own children and farewell parents).

Man exit:

Exit has done these things:

1. call all functions registered by it (Exit Function) in the reverse order of Axexit or on_exit registration, and you can think of On_exit as an extension of atexit. (This allows us to specify that we perform our own cleanup actions when the program terminates. For example, save the program state information to a file, unlock the lock on the shared database, and so on. If any of the registered functions do not return (such as the function call _exit or kill themselves with a signal like Sigkill), then the remaining registration functions will not be executed, and the next deeper exit will not be executed. If a function is registered more than once, it is also executed sequentially (these are the atexit features).

2. All open input and output streams are emptied and closed, in other words, the contents of the buffer are written back to the file. Temporary files created with the Tmpfile function will be removed.

3. Call _exit.

So, in general, the exit is to _exit some packaging, making the entire exit process is not so rough, they both have a common point is to close the file descriptor, will empty the memory, but exit will also empty the input and output stream cache, remove the temporary files created, Call the registered Exit function and so on.

Now we just need to use a small example to see the difference between the two:

 #include <stdio.h> #include  <stdlib.h>  #include  <unistd.h>int  Main (int  argc, char   Const  *argv[]) {printf (  " hello\n   " ); printf (  "  hi   "     0   // _exit (0);  return  0  ;}  

For the above code, our execution results are:

And we changed the code:

 #include <stdio.h> #include  <stdlib.h>  #include  <unistd.h>int  Main (int  argc, char   Const  *argv[]) {printf (  " hello\n   " ); printf (  "  hi   "     // exit (0);     _exit (0   return  0  ;}  

The result becomes:

This is obviously because the _exit does not perform operations such as buffer emptying, and exit clears the buffered contents.

Reference blog:

Http://blog.chinaunix.net/uid-12461657-id-3140887.html

The difference between Linux exit and _exit

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.