The difference between exit, _exit, and return in the termination of the process


Header file: stdlib.h

Function declaration: void exit(int status);

Parameter description: status ----> the exit status of the process, the parent process can use its lower 8 bits.

Return value: is returned to the parent process, usually used to indicate the success or failure of the task completed by the process. If it succeeds, it returns 0; if there is an error, it returns a
non-zero value.

Role: 1. End the execution of the process;

           2. Close all open files;

           3. If the parent process is in the wait state, exit will restart the parent process to run;

           4. Complete some internal cleaning tasks of the system, such as clearing buffers.

          In general: exit is used to end the entire running program, return parameters to the operating system, and transfer control to the operating system.


Header file: stdlib.h

Function declaration: void _exit(int status);

Similar to exit, the difference is that it executes the action of terminating the process without cleaning up the system internally, so it is generally not used.

3. The return statement

    1. Put it in the main function to end the process.

    2. Indicates that the execution continues from the called function to the calling function, and a return value can be attached when returning, which is specified by the parameter after return.

    In general: return is to exit the current function, return the function value, and transfer control to the calling function.

Guess you like