[What's the difference between Linux]read/write and Fread/fwrite

Source: Internet
Author: User
Tags fread

Transferred from: http://blog.csdn.net/xiaofei0859/article/details/51145051

Both are working on the file, so what's the difference between the two, and how do you choose when you use it?

1. Differences
    • The fread is buffered and read without buffering.
    • Fopen is defined in standard C, and open is defined in POSIX.
    • Fread can read a structure. Read in Linux/unix There is no difference between reading binary and normal files.
    • Fopen cannot specify permissions to create a file. Open can specify permissions.
    • fopen returns a pointer, open returns a file descriptor (integer).
    • Any device in Linux/unix is a file and can be used open,read.

if the size of the file is 8k.

If you use Read/write and only allocate 2k of cache, you will need to make 4 system calls to read this file to actually read from disk.

If you use Fread/fwrite, the system automatically allocates the cache, and the read out of this file is read out of the disk as soon as the system call is made.

That is to read the disk 4 times with Read/write, while using Fread/fwrite to read 1 times. Efficiency is 4 times times higher than Read/write.

If the program is restricted internally, it is better to use Read/write.

With Fread and fwrite, it automatically allocates caches, which can be very fast and simpler than what you do. If you want to handle some special descriptors, use read and write, such as socket, pipe, etc.

The efficiency of the system call write is determined by the size of your buf and the total number of writes you want to write, and if the buf is too small, you get into the kernel space a lot and your efficiency is low. And fwrite will cache for you, reducing the actual system calls, so the efficiency is higher.

If it is called only once (possibly?), the two are almost, strictly speaking, a little bit faster (because in fact fwrite is actually using write to do the real file system work), but the difference doesn't matter.

2. Explanations from the Forum

Fread with cache refers to the application layer with the cache, read is also a cache but refers to the system layer or kernel layer, of course, may not take, such as direct DMA, driven by the driver.

Fread is the buffer of the standard library, and read is the buffer of the kernel.

Read/write is more efficient than fread/fwrite if it can precisely control the data read and written once

Read/write corresponds to System call in Linux, while Fread/fwrite is another encapsulation of read/write, read/write more primitive, if cross-platform is not considered, it is recommended to use more Read/write .

As an example :

Take the following steps:

    1. Open File
    2. Read or fread the 0k~4k of the file
    3. Other operations
    4. Read or fread the 1k~3k of the file
    5. Close File

At this time, if it is read, step 4 to call the kernel, and if it is fread, because step 2 in the application layer has buffered the required content, the data will be returned directly without having to call the kernel again

[What's the difference between Linux]read/write and Fread/fwrite

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.