Exit in Linux

Source: Internet
Author: User
Tags exit in posix terminates

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 functions are described as follows:

    void exit (int status)

    The function terminates the calling program. status passed to the system for parent process recovery. Before the program exits, exit () call all the Span style= "FONT-FAMILY:CALIBRI;" >atexit () registered functions, emptying all open < stdio.h> file* stream the buffer and close the stream, and then delete all the tmpfile () temporary files created. When the process exits, the kernel closes all remaining open files (that is, those by open () , creat () or file descriptor inherits open files, frees its address space, and frees all other used resources. exit () never return.

void _exit (int status)

This function is basically the same as the POSIX _exit () function .

int atexit (void (*function) (void))

functions are a function pointer to a callback function that is called when the program exits. exit () calls the callback function before it closes the file and terminates. The idea is that the program can provide one or more cleanup functions to run before the final shutdown. Provides a function to be registered with the function.

Atexit () returns 0on Success, returns 1 when an error occurs, and sets the corresponding errno.

The following program does not have useful functionality, but it demonstrates how to use the 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);

}

here is the result of the program running:

$atexit

Registering Callback1

Registering Callback2

Registering Callback3

Exiting now

Callback3 called

Callback2 called

Callback1 called

    atexit () Registered functions run in the opposite order of registration: Most recently registered (this is also known as LIFO ( lifo ).

POSIX defines the _exit () function. unlike exit () ,exit () invokes the callback function and makes <stdio.h> cleanup,_ Exit () is a function of " immediate death " :

#include <unistd.h>

void _exit (int status);

_exit terminates the calling process, but does not close the file, does not clear the output cache, and does not call the egress function. the exit function terminates the calling process. Before exiting the program, all files are closed, the buffered output refreshes the definition, and all flushed " exit functions "(defined by atexit ) are called.

    actually, iso C _exit () function and _exit () Span style= "font-family: ' The song Body '; > same. c function indicates _exit () whether to invoke atexit () Span style= "font-family: ' The song Body '; > Register the function and close the open file depending on the implementation. For glibc system, probably not, i.e. _exit () and _exit ()

     Use the _exit () fork () exec () when it failed. In this case, you do not need to use the usual exit () file*

For example, join you to run a Shell command, and call it yourself Fork () and the exec () . The code might look like this:

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 take the custom used by the POSIX Shell . If the requested program does not exit ( An entry in the enoent--directory that does not have it), the exit value is 127. Otherwise, the file exits, but for other reasons it cannot be executed by exec () , the exit status is 126. It would be a good idea to take this practice in your own program.

in short, in order to better use exit () and the atexit () , you should follow the rules:

1. Define a small collection of exit status values that your program uses to communicate with its callers using the values in that collection. Use #define constants or enums to define these values in your code.

2.decide if it is necessary to use the callback function with atexit () . If necessary, register these functions in the appropriate place in main () , for example, after parsing the options and initializing any data structures that the callback function might erase, remember that the function is LIFO( Last-in-first-out ) in order to make the call.

3.If there is an error, you can exit from the program using Exit () in either place , and exit is the correct behavior that can occur. Also use the error code that you defined.

    4main () function is an exception where you can use the Span style= "FONT-FAMILY:CALIBRI;" >return exit () main () at the end, use Span style= "FONT-FAMILY:CALIBRI;" > "return 0"

5.if calling exec () fails, use _exit () or _exit ()in the child process.


This article is from the "11275984" blog, please be sure to keep this source http://11285984.blog.51cto.com/11275984/1792913

Exit in Linux

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.