Linux C language exit function explanation

Source: Internet
Author: User
Linux C language exit function explanation The C standard defines the following exit functions: # Include <stdlib. h> void exit (INT status); void _ exit (INT status); int atexit (void (* function) (void); function introduction: void exit (INT status) The function stops calling Program . Status is passed to the system for parent process recovery. Before exiting the program, exit () calls all functions registered with atexit () to clear all opened <stdio. h> file * the buffer of the stream, close the stream, and delete all temporary files created by tmpfile. When a process exits, the kernel closes all the remaining opened files (that is, those files inherited from open (), creat (), or file descriptor) and releases the address space, release all other resources. Exit () never returns. Void _ exit (INT status) this function is basically the same as POSIX's _ exit () function. We will introduce it later. Int atexit (void (* function) (void) function is a function pointer that points to a callback function called when the program exits. Exit () calls the callback function before it closes the file and ends. The idea is that the program can provide one or more running cleanup functions before it is finally closed. Provide a function to be registered. If atexit () is successful, 0 is returned. If an error occurs,-1 is returned and the corresponding errno is set. The following program does not have any useful functions, but it demonstrates how to use atexit (): void callback1 (void) {printf ("Callback called \ n");} void callback2 (void) (printf ("Callback called \ n");} void callback3 (void) (printf ("Callback called \ n");} int main (INT argc, char ** argv) {printf ("registering callback1 \ n"); atexit (callback1); printf ("registering callback2 \ n"); atexit (callback2 ); printf ("registering callback3 \ n"); atexit (callback3); printf ("exiting now \ n"); exit (0);} The following is the program running result: $ Atexit Registering callback1 registering callback2 registering callback3 exiting now callback3 called callback2 called callback1 called as shown in the preceding example, the running sequence of the function registered with atexit () is the opposite of the registration sequence: recently registered first run (this is also called last-in-first-out (LIFO ). POSIX defines the _ exit () function. Unlike exit (), exit () calls the callback function and performs <stdio. h> cleanup, _ exit () Is the function of "immediate death": # include <unistd. h> void _ exit (INT status); _ exit ends the calling process, but does not close the file, clear the output cache, or call the exit function. The exit function terminates the calling process. Before exiting the program, all files are closed. The buffered output content refreshes the definition and calls all refreshed export functions (defined by atexit ). In fact, the _ exit () function of Iso c is the same as _ exit. C function indicates whether _ exit () calls a function registered with atexit () and closes the opened file depends on the implementation. For glibc systems, it may not, that is, _ exit () and _ exit () are similar.       The time when _ exit () is used is when exec () fails to be called in the child process generated by fork. In this case, the usual exit () is not required because it clears all the buffer data saved by the file * stream. When the parent process clears its buffer copy, the buffered data is written twice. Obviously, this is not appropriate.       For example, you have added a shell command and called fork () and exec () by yourself ().CodeIt may be as follows:       Char * shellcommand = "...";       Pid_t child;       If (child = fork () = 0 ){                
Execl ("/bin/sh", "sh", "-c", shellcommand, null );                
_ Exit (errno = enoent? 127:126 );       }             The errno test and exit values adopt the POSIX shell convention. If the requested program does not exit (the enoent -- directory does not have its entry), the exit value is 127. Otherwise, the file exits, but cannot be executed by Exec () for other reasons, and the exit status is 126. It will be a good idea to adopt this convention in your own program. In short, to better use exit () and atexit (), you should follow the rules below: 1. define a set of smaller exit Status values, your program uses the values in the set to communicate with the caller. Use # define constant or enum to define these values in your code. 2. Determine whether it is necessary to use the callback function with atexit. If necessary, register these functions as appropriate in main (). For example, after parsing options and initializing any data structures that the callback function may clear, remember to call a function in the order of LIFO (last-in-first-out. 3. If an error occurs, exit from the program using exit () wherever possible. exit is the correct action. Use the error code you defined at the same time. 4. The main () function is an exception. You can use return in it. In our own style, exit () is usually used when there is a problem. If everything is normal, use "Return 0" at the end of main ". 5. If exec () fails to be called, use _ exit () or _ exit () in the child process ().

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: 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.