Several forms of process exit under Linux

Source: Internet
Author: User
Tags terminates

Process Exit

The process exit of Linux is divided into two types: normal exit and abnormal exit.

1. Normal exit

A. Execute return in the main () function.

B. Call the exit () function

c. Call the _exit () function  

2. Abnormal exit

A. Calling the About function

B. The process receives a signal which causes the program to terminate.  

regardless of the exit method, the system will eventually execute the same code in the kernel. This code closes the open file descriptor used by the process, freeing the memory and other resources it consumes.  

comparison of several exit modes

The difference between 1.exit and return:

Exit is a function that has parameters. Leave control to the system after exit executes

Return is returned after the function has finished executing. Renturn the control to the calling function when the execution is complete.

The difference between 2.exit and abort:

Exit is the normal termination process

About is an abnormally terminated.  

exit () and _exit () functions

The 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, and terminates the operation of this process.

exit is declared in the header file Stdlib.h, and the _exit () declaration 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 exit () and _exit ()

_exit () returns to the kernel immediately after execution, and exit () performs some cleanup before handing control over to the kernel.

When the _exit function is called, it closes all the 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.

take a look at the difference between them through a function instance:  

function Instance 1:exit.c  

#include <stdio.h>#include<stdlib.h>int  main () {    printf ("  Using exit----\ n");    printf ("This was thecontent in buffer\n");    Exit (0

The result of the execution is:

Using exit----

The content in buffer  

function Instance 2:_exit.c  

#include <stdio.h>#include<stdlib.h>int  main () {    printf ("  Using _exit--\n");    printf ("Thecontent in buffer");    _exit (0

The result of the execution is:  

using _exit--  

The printf function is the way to use buffered I/O, which automatically reads the record from the buffer when it encounters a "\ n" line break. So exit () writes out the buffer's data before exiting, and the _exit () function exits directly.  

You can also put the function in Example 2 of printf ("This was the content in buffer"), instead of printf ("This is the content in buffer\n") (that is, in printf last add a \ n See what the running results are, and why do they produce such results? ) 

The order in which the parent-child process terminates will produce different results

1. The parent process terminates before the child process:

This situation is the orphan process we used earlier. When the parent process exits first, the system causes the INIT process to take over the child process.

2. The child process terminates before the parent process, and the parent process does not call the wait function

in this case, the child process goes into a zombie state and will persist until the system restarts. When a child process is in a zombie state, the kernel only saves some of the necessary information for the process in case the parent process needs it. At this point the child process always occupies resources and reduces the maximum number of processes that the system can create.  

What is a zombie state?  

A process that has been terminated, but whose parent process has not yet dealt with it (getting information about terminating the child process, releasing the resources it still occupies) is called the Zombie process (zombie).

3. The child process terminates before the parent process, and the parent process calls the wait function

At this point the parent process waits for the child process to end.

Several forms of process exit under Linux

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.