The relation and difference between Linux C _exit function and Exit function

Source: Internet
Author: User

First, contact

1. Functionally, the _exit and exit functions are to let the process exit gracefully, that is, to close the file descriptor opened by the process, freeing up occupied memory and other resources.

Second, the difference

1. The _exit function is declared in the header file Unistd.h, and exit is declared in the header file stdlib.h.

2. After executing the _EXIT function, control immediately returns to the kernel, and the Exit function performs some cleanup before handing over control to the kernel.

3. The _exit function does not flush I/O buffers and may result in data loss, and the Exit function is a wrapper over the _exit function, which refreshes the I/O buffer before calling the _exit function, guaranteeing the integrity of the data.

Note: Knowledge background: I/O buffers

In the Linux C standard library, a technique called "I/O buffers" is used, that is, for each open file, a read-write buffer is opened in memory. When reading a file, will be continuously read from the hard disk a number of data to the buffer, the next time you read the file directly from the buffer to get data, as well, when writing a file, the data is written to the buffer, until the buffer of the amount of data to a certain degree or receive a special instruction, The data in the buffer is then written to the hard disk once. This technology reduces the number of times the program accesses the hard drive and improves operational efficiency.

Iii. Conclusion

  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 function, and writes the contents of the file buffer back to the file.

This conclusion is verified by two demo programs below.

1. Call the Exit function to end the program

1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>//exit ()4 5 intMainintargcChar*argv[])6 {7printf"first_line\n");//the function flushes the buffer when it encounters "\ n"8printf"Second_line");9Exit0);Ten     return 0; One}

The result of the operation is as follows: Both lines are printed.

  

2. Call the _exit function to end the program

1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMainintargcChar*argv[])6 {7printf"first_line\n");8printf"Second_line");9_exit (0);Ten     return 0; One}

The result of the operation is as follows: Just print out the first line

  

3. Flush the buffer first, then call the _exit function to end the program

1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMainintargcChar*argv[])6 {7printf"first_line\n");8printf"second_line\n");9_exit (0);Ten     return 0; One}

The result is as follows: combined with Demo2 and DEMO3, it can be concluded that Demo2 did not print out the second line, because the _exit function ended the program without doing a flush buffer operation, resulting in data loss.

  

The relation and difference between Linux C _exit function and Exit function

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.