Difference between Linux exit command and _exit command

  
                

In the Linux system command, the exit command and the _exit command seem to be the same, but the difference between the exit command and the _exit command is different. Let's get to know it.

NOTE: exit () is the exit status code parameter passed when the program exits, 0 represents a normal exit, the other represents a non-normal exit, usually with 1 or -1, standard C There are two macros EXIT_SUCCESS and EXIT_FAILURE, with exit (EXIT_SUCCESS); readability is better.

As a system call, _exit and exit are a pair of twin brothers. To what extent they are similar, we can find the answer from the Linux source code:

#define __NR__exit __NR_exit /* Excerpt from the file include/asm-i386/unistd.h line 334*/

“__NR_” is the prefix for each system call in the Linux source code, please note that there is before the first exit 2 underscores, only one underline before the second exit. Anyone who knows C and is clear-headed will say that there is no difference between _exit and exit, but let's talk about the difference between the two, which is mainly reflected in their definition in the library. The prototype of _exit in the Linux library is:

#i nclude "unistd.h" void _exit(int status);

Compared with exit, the exit() function is defined in stdlib. h, and _exit() is defined in unistd.h. From the name, stdlib.h seems to be a bit higher than unistd.h. So, what is the difference between them? The _exit() function is the simplest: to stop the process directly, clear the memory space it uses, and destroy its various data structures in the kernel; the exit() function makes some wrappers on these foundations, Some steps have been added before the exit, and for this reason, some people think that exit is not a pure system call. The biggest difference between the exit() function and the _exit() function is that the exit() function checks the opening of the file before calling the exit system call, and writes the contents of the file buffer back to the file, that is, "cleans up I/O". Buffer & rdquo;.

exit() Before ending the process that called it, perform the following steps:

1. Call the function registered by atexit() (export function); call in the reverse order of ATEXIT registration All the functions registered by it, which allows us to specify to perform our own cleanup actions when the program terminates. For example, save program state information to a file, unlock locks on a shared database, and so on.

2.cleanup(); closes all open streams, which will cause all buffered output to be written, and all temporary files created with the TMPFILE function will be deleted.

3. Finally, the _exit() function is called to terminate the process.

_exit does 3 things (man): 1, Any open file descriptors belonging to the process are closed 2,any children of the process are inherited by process 1, init 3, the process‘s parent is sent a SIGCHLD signal

exit After the cleanup is done, _exit is called to terminate the process.

In addition, another explanation:

Simply put, the exit function will terminate the calling process. Before exiting the program, all files are closed, the buffered output will refresh the definition, and all refreshed <export functions" (defined by atexit) will be called.

_exit: This function is defined by Posix and does not run exit handlers and signal handlers. It does not flush standard I/O streams on UNIX systems.

Simply put, _exit terminates the calling process, but does not close the file, does not clear the output cache, and does not call the exit function.

Common:

Regardless of how the process terminates, the kernel will close all file descriptors opened by the process, freeing the memory used by the process!
Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved