I. The main difference between the exit function and the return function is that exit is used to end the program at any time while the program is running. its parameters are returned to the OS. You can also say that the exit function exits the application and
I. The main differences between the exit and return functions are:
Exit is used to end the program at any time while the program is running. its parameters are returned to the OS. You can also say that the exit function exits the application and returns a state of the application to the OS, which identifies some running information of the application.
The exit function is also called implicitly at the end of the main function. when the exit function is run, it first executes the function registered by the atexit () function and then cleans itself, refresh all output streams, close all open streams, and close temporary files created using the standard I/O function tmpfile.
Exit indicates the completion of a process at the system call level. it deletes the memory space used by the process and returns the error message to the parent process. Generally, exit (0) indicates that the program is normal, exit (1) and exit (-1) indicate that the program exits abnormally, and exit (2) indicates that the system cannot find the specified file.
Return indicates the return of the call stack. return indicates the return of the function value and exit the function. generally, 0 indicates normal exit, and non-0 indicates abnormal exit. Please note that, if it is in the main function, it will naturally end the current process (that is, in main (), you can use return n or exit (n) directly ), if it is not in the main function, it is returned to the previous call. In multiple processes, if you want to check whether the previous process exits normally, you need to use the return value of the previous process.
2. process environment and process control
If exit (int n) is called in the main function, exit (int n) and return an int value. Generally, run a program under the shell and run the echo $? Command? The return value of the program, that is, the exit value. Theoretically, exit can return any integer less than 256, and different values are mainly processed differently by the caller.
The return value of exit for a separate process is returned to the operating system, but if it is a multi-process, it is returned to the parent process. The parent process calls functions such as waitpid () to get the exit status of the child process for different processing. The caller can process the request based on the returned values.
In general, exit (int n) is the current process that returns its control to the main program that calls the subroutine. The Returned values in the brackets tell the calling program the running status of the program.
1. process start:
The C program is executed from the main function. the prototype is as follows:
int main(int argc, char *argv[]);
Generally, the return value of main is int type, and 0 is returned correctly.
2. process termination:
There are two types of termination of C program: normal termination and exceptional termination.
Normal termination is divided into return, exit, _ exit, _ Exit, pthreade_exit.
Exception: abort, SIGNAL. the thread response is canceled.
It mainly refers to the first four types of normally terminated functions, namely exit functions.
#include
void exit(int status); void _Exit(int status); #include
void _exit(int status);
The differences between the above three functions are:
Exit () (or return 0) will call the standard I/O cleanup programs (such as fclose) that terminate the processing program and the user space ), while _ exit and _ Exit are not called and are directly taken over by the kernel for cleaning. Therefore, exit (0) in the main function is equivalent to return 0.
3. atexit:
Iso c requires 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 registered multiple times, it will also be called multiple times.
The prototype is as follows:
# Include Int atexit (void (* func) (void ));
The parameter is a function pointer pointing to the end processing function. this function has no parameters and no return value. After the atexit function is successfully called, 0 is returned.
The following procedure is used as an example:
# Include
Static void myexit1 () {printf ("first exit handlern");} static void myexit2 () {printf ("second exit handlern");} int main () {atexit (my_exit2); atexit (my_exit1); atexit (my_exit1); printf ("main is donen"); return 0; // equivalent to exit (0 )}
Running result:
$ ./a.outmain is done first exit handler first exit handler second exit handler
The exit function is also called implicitly at the end of the main function. when the exit function is run, it first executes the function registered by the atexit () function and then cleans itself, refresh all output streams, close all open streams, and close temporary files created using the standard I/O function tmpfile.
Note the results of the above program, you can find that these functions are automatically called in the reverse order of registration by exit (myexit1 first and myexit2 ). If the same function is registered multiple times, it will also be called multiple times (for example, myexit1 ).
These processing functions call these processing functions when the program exits. However, if you exit the program with _ exit (), it does not close any files, clear any buffers, or call any termination function!