开发者

What's the difference between exit and _exit both in libc.so?

开发者 https://www.devze.com 2023-02-22 10:53 出处:网络
(gdb) info symbol exit exit in sec开发者_运维问答tion .text of /lib64/libc.so.6 (gdb) info symbol _exit
(gdb) info symbol exit
exit in sec开发者_运维问答tion .text of /lib64/libc.so.6
(gdb) info symbol _exit
_exit in section .text of /lib64/libc.so.6

Anyone knows?


Simply said, exit is a high-level function that you should call to exit a process, it invokes on-exit handlers and some other high-level cleanup stuff. _exit is a low-level cleanup function, it is called as a last step from exit. exit will really terminate the process (by making the exit syscall).

From the glibc manual at http://www.gnu.org/software/libc/manual/html_mono/libc.html (also check out the source code of glibc for details):

25.6.1 Normal Termination

A process terminates normally when its program signals it is done by calling exit. Returning from main is equivalent to calling exit, and the value that main returns is used as the argument to exit.

— Function: void exit (int status)

The exit function tells the system that the program is done, which causes it to terminate the process.

status is the program's exit status, which becomes part of the process' termination status. This function does not return.

Normal termination causes the following actions:

Functions that were registered with the atexit or on_exit functions are called in the reverse order of their registration. This mechanism allows your application to specify its own “cleanup” actions to be performed at program termination. Typically, this is used to do things like saving program state information in a file, or unlocking locks in shared data bases.

All open streams are closed, writing out any buffered output data. See Closing Streams. In addition, temporary files opened with the tmpfile function are removed; see Temporary Files.

_exit is called, terminating the program. See Termination Internals.

And in section "Termination Internals":

25.6.5 Termination Internals

The _exit function is the primitive used for process termination by exit. It is declared in the header file unistd.h.

— Function: void _exit (int status)

The _exit function is the primitive for causing a process to terminate with status status. Calling this function does not execute cleanup functions registered with atexit or on_exit.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号