Linux multi-task programming Four: Exit () function and its basic experiment

Source: Internet
Author: User

Source: CSDN Wang Wensong transferred from Linux commune

Exit () and _exit () functions

Function description

The create process uses the fork () function, the execution process uses the EXEC function family, and the terminate process uses the exit () and _exit () functions. When the process executes to the exit () or _exit () function, the process unconditionally stops all remaining operations, clears the various data structures, and terminates the operation of the process. However, the two functions are still different, and call procedure 1 as follows:

As can be seen from Figure 1, the _exit () function is to directly stop the process from running, clear its used memory space, and clear its various data structures in the kernel, while the exit () function makes some wrappers on these bases, adding several operations before exiting. The most important difference between the exit () function and the _exit () function is that the exit () function checks which files the process has opened before terminating the current process, and writes the contents of the file buffer back to the file, which is the "clean I/O buffer" item in Figure 1.

In the standard library of Linux, there is an operation called buffered I/O (buffered I/O), 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 you write a file, it is only written in memory buffer, and so on to meet certain conditions (such as reaching a certain number or encounter a certain character, etc., the most typical is our vim used w command), and then write the contents of the buffer once to the file.

This technique greatly increases the speed of file reading and writing, but it also brings some problems to our programming. For example, some data you think have been written to the file, in fact, because it does not meet the specific conditions, they are only saved in the buffer, then use the _exit () function directly to shut down the process, the data in the buffer is lost. Therefore, if you want to ensure the integrity of the data, it is best to use the exit () function.

function syntax

The following table lists the syntax points for the exit () and _exit () functions:

Basic experiments

The following two basic experiment 1 compared the exit () and _exit () functions. Since the printf () function uses buffered I/O, the function automatically reads the record from the buffer when it encounters a "\ n" line break, and the following two basic experiments are compared using this property. The following is the code for Experiment 1:

Execution results such as

As you can see from the output, when you call the exit () function, the records in the buffer can also be output normally.

The code for Experiment 2 is as follows:

Execution results such as:

As you can see from the final result, calling the _exit () function does not output the record in the buffer.

If you add a carriage return to the second code in the Code in Experiment 2, the result will be different. Try it yourself, huh!

Linux multi-task programming Four: Exit () function and its basic experiment (GO)

Related Article

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.