Linux Multi-task programming (four) exit () function and its basic experiment

Source: Internet
Author: User
Tags data structures

Exit () and _exit () functions

Function description

The create process uses the fork () function, the execution process uses the EXEC function family, and the exit () and the _exit () function are used to terminate the process. When a process executes to the exit () or _exit () function, the process unconditionally stops all remaining operations, clears the various data structures, and terminates the running of the process. However, there is a difference between the two functions, and the invocation process is as shown in Figure 1:

As you can see from Figure 1, the _exit () function is to simply stop the process, clear the memory space it uses, and clear its various data structures in the kernel, while the exit () function does some packaging on these foundations, adding several operations before executing the exit. The biggest 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 in Figure 1.

In Linux's standard function library, there is an operation called buffered I/O (buffered I/Os), which is characterized by a buffer in memory for each open file.

Every time you read a file, several records are read consecutively, so that the next time you read the file, you can read it directly from the memory buffer; Similarly, every time you write a file, it is only written in memory buffer, and so satisfied with certain conditions (such as reaching a certain number or encountered a certain character, etc., the most typical is our vim used: w command), and then write the contents of the buffer to the file at once.

This technology greatly increases the speed of file reading and writing, but it also brings some trouble to our programming. For example, some data you think has been written to the file, in fact, because the specific conditions are not met, they are only stored in the buffer, then use the _exit () function to directly shut down the process, the data in the buffer will be 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 experiment

The following two basic experiment 1 compares the difference between exit () and _exit () functions. Because the printf () function uses buffered I/O, the function is automatically read from the buffer when it encounters the "\ n" newline character, and the following two basic experiments use this property to compare. The following is the code for Experiment 1:

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.