To create a process with the fork () function, how do you terminate a process?
Process exits are categorized as normal exit and exception exit:
Normal exit:
1). Execute return in the main () function.
2). Call the exit () function
3). Call the _exit () function
An abnormal exit may call a function or receive some kind of signal.
Now only the normal exit is discussed:
(1) The difference between exit and return:
A.exit is a function that has parameters. Leave control to the system after exit executes
B.return is the return after the function has finished executing. Renturn the control to the calling function when the execution is complete.
Exit () and _exit ()
Both the 1.exit and _exit functions are used to terminate the process.
When the program executes to exit or _exit, the system unconditionally stops all remaining operations, clears the various data structures including the PCB, and terminates the operation of this process.
2.exit is declared in the header file Stdlib.h, and _exit () is declared in the header file unistd.h. The argument Exit_code in exit is 0 for normal termination of the process, and if other values indicate an error occurred during program execution.
The difference between 3.exit () and _exit ():
A._exit () returns to the kernel immediately after execution, and exit () performs some cleanup before handing control over to the kernel.
B. When the _exit function is called, it closes all file descriptors of the process, cleans up memory and some other kernel cleanup functions, but does not flush the stream (stdin, stdout, stderr ...). The Exit function is a wrapper over the _exit function that calls _exit and flushes the stream before the call.
The most important difference between the exit () function and the _exit () function is that the exit () function checks the opening of the file before calling the exit system, and writes the contents of the file buffer back to the file. Because of the standard library of Linux, there is an operation called "Buffered I/O", which is characterized by a buffer in memory for each open file. Each time a file is read, a number of records will be read sequentially, so that the next time you read the file can be read directly from the memory buffer, also, each time the file is written to the memory buffer, and so on to meet certain conditions (such as reaching a certain number or encountering a certain character, etc.), The contents of the buffer are then written to the file once. This technique greatly increases the speed of file read and write, but it also gives the programmer a bit of trouble. For example, there are some data that have been written to the file, in fact, because they do not meet the specific conditions, they are only in the buffer, then use the _exit () function to close the process directly, the buffer data will be lost. Therefore, to ensure the integrity of the data, you must use the exit () function.
This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1761915
The difference between the Linux process exit--exit and return