About Return and exit

Source: Internet
Author: User

About Return and exit

 There are two ways to exit the child process, exit and exec family functions, can not use return, why not return, exit to return will appear the parent-child process and each repeat began.

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

1) 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. In the entire program, the call to exit ends as long as it is called.  

2) Return is the language level, which represents the return of the call stack, return is returning the function value and exiting 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.

2. Process environment and Process Control

Exit (int n) is actually the direct exit program, because the default standard program entry is int main (int argc, char** argv), the return value is int type. 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 () 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.

About Return and exit

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.