The differences between the functions of exit, return, _exit, _exit are quoted

Source: Internet
Author: User
Tags exit in

The main differences between the Exit function and the return function are:

Exit is used to end the program at any time while the program is running, and its parameters are returned to the OS. You can also say this: The Exit function exits the application and returns a state of the application to the OS, which identifies some of the application's operational information.

The Exit function is also implicitly called at the end of the main function, and the Exit function runs with a function that is enlisted by the atexit () function, and then does some of its own cleanup, flushing all output streams, closing all open streams, and closing through standard I/O functions tmpfile () Creates a temporary file.

Exit is the system call level, which represents the end of a process that deletes the memory space used by the process and returns the error message to the parent process. Typically: Exit (0) indicates that the program is OK, exit (1) and exit (-1) indicate that the program exited unexpectedly, and exit (2) indicates that the system cannot find the file specified.

Return is a language level that represents the return of the call stack, return returns the function value and exits the function, usually 0 for normal exit, not 0 for abnormal exit, note that if it is in main function main, it will naturally end the current process (i.e., in main (), You can do it with return N, or directly with exit (n), if not in the main function, that is to return to the previous layer of the call. In the case of multiple processes, the return value of the previous process is used if it is sometimes necessary to detect whether the previous process exited normally.

II. process environment and Process Control

if exit (int n) is called in the main function, exit (int n) exits the program directly and returns a value of type int. Typically under the shell, run a program and then use the command echo $? To get the return value of the program, which is the exit value. In theory, exit can return any integer less than 256, and the different values returned are primarily for the caller to handle differently.

The return value for a separate process exit is returned to the operating system, but if it is multi-process, it is returned to the parent process. A function called Waitpid () inside the parent process gets the status of the child process exit so that it can be handled differently. The caller is given the appropriate processing according to the corresponding return value.

In general, exit (int n) is the current process that returns its control to the main program that called the subroutine, with the return value in parentheses telling the calling program the running state of the program.

1. Start of the process:

The C program starts from the main function and is prototyped as follows:

int main (int argc, char *argv[]);

Normally the return value of main is int and returns 0 correctly.

2. Process termination:

The termination of the C program is divided into two types: normal termination and abnormal termination.

Normal termination is divided into: return, exit, _exit, _exit, Pthreade_exit.

The exception middle finger is divided into: Abort, SIGNAL, thread response cancellation.

Mainly about the normal termination of the first 4 kinds, that is, the Exit series function.

#include <stdlib.h> void exit (int status); void _exit (int status); #include <unistd.h> void _exit (int status);

The differences between the above 3 functions are:

Exit () (or return 0) invokes a standard I/O cleaner (such as fclose) that terminates the handler and user space, while _exit and _exit are not invoked and are directly cleaned up by the kernel takeover. Therefore, exit (0) in the main function is equivalent to return 0.

3. Atexit terminate the handler:

ISO C stipulates that a process can register up to 32 termination handler functions, which are automatically called by exit in the reverse order of registration. If the same function is enlisted multiple times, it is also called multiple times.

The prototype is as follows:

#include <stdlib.h> int atexit (void (*func) (void));

Where the parameter is a function pointer, pointing to the terminating handler function, which has no parameter and no return value. The Atexit function itself returns 0 after a successful call.

Take the following procedure as an example:

#include <stdlib.h> static void Myexit1 () {printf ("First exit Handlern"),} static void Myexit2 () {printf ("second Exi T Handlern ");} int main () {atexit (my_exit2); atexit (my_exit1); atexit (my_exit1); printf ("Main is Donen"); return 0;//equivalent to exit (0)}

Operation Result:

$./a.outmain is do first exit handler first exit handler second exit handler

The Exit function is also implicitly called at the end of the main function, and the Exit function runs with a function that is enlisted by the atexit () function, and then does some of its own cleanup, flushing all output streams, closing all open streams, and closing through standard I/O functions tmpfile () Creates a temporary file.

Note the results of the above program, you can find that these functions are automatically called by exit in the reverse order of registration (first myexit1 after Myexit2). If the same function is registered multiple times, it is also called multiple times (as myexit1 here).

These handler functions are called with the Atexit function when the program exits. However, if you exit the program with _exit (), it does not close any files, does not clear any buffers, and does not invoke any terminating functions!

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