Buffered I/O vs. unbuffered I/O (fopen differs from open series functions)

Source: Internet
Author: User

In the program, for the file read and write, the program run time is mainly consumed on the I/O. Compared to read/write memory, the files on the read-write drive are much slower, and the program run time is mostly consumed on I/O when there is little content to read/write to the file. Therefore, buffered I/O and non-buffered I/O are available.

Non-buffered I/O, each time for the operation of the file, I/O operations. For example Linux system strips are used:

Open, read, write, Lseek, close.

They rely on the operating system, do not have a file structure pointer (the file as a binary file), directly manipulate the file, these operations do not pass through the buffer. For example, write a file, write the content directly to the physical disk. The advantage is the ability to quickly write content to a file, in the event of an emergency (such as a power outage), the content will not be lost. The disadvantage is the frequent I/O operation, which consumes CPU.

fopen, Fclose, Fread, Fwrite, fgetc, Fgets, FPUTC, fputs, Freopen, fseek, Ftell, rewind

buffer I/O, is to open a buffer in memory, the operation of the file has a buffer, the "need" when the write to disk. In general, the larger the buffer, the less actual I/O is, and the more efficient it is.

They depend on the file struct pointer, which can read characters, strings, and formatted data.

The function of standard I/O is buffered I/O operations:

Buffered I/O has full buffering and row buffering:

Full buffering is the actual I/O operation after the buffer fills up. Files on disk are often fully buffered by standard I/O library functions.

A row buffer is the actual I/O operation when a newline character is encountered in the input or output.


difference:

The Fopen series functions are provided by the standard library, using the file structure pointer to process the file as a stream.

The Open Series function is a system bar, using a file descriptor.

Everything in Linux is file, but fopen only refers to ordinary files. Device files cannot be processed as streaming files, only the open series functions can be used.

Fopen enables the process to have a buffer in the user state, unlike the open series functions, each time the system bar is used, the user state/kernel state switch.

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.