'What's the difference between read and fread?

I have a question about the SO question What is the difference between read() and fread()?

How do read() and fread() work? And what do unbuffered read and buffered read mean?

If fread() is implemented by calling read(), why do some people say fread() is faster than read() in the SO question C — fopen() vs open()?



Solution 1:[1]

fread cannot get faster than read provided (!) you use the same buffer size for read as fread does internally.

However every disk access comes with quite some overhead, so you improve performance if you minimise their number.

If you read small chunks of data then every read accesses the disk directly, thus you get slow, while in contrast fread profits from its buffer as long as there's yet data in – and on being consumed up the next large chunk is read into the buffer at once to again provide small chunks from on being called again.

Solution 2:[2]

What's the difference between read and fread?

fread is a standardized C programming language function that works with a FILE pointer.

read is a C function available on a POSIX compatible system that works with a file descriptor.

how do read() and fread() work?

fread calls some unknown system defined API to read from a file. To know how specific fread works you have to look at specific implmeentation of fread for your specific system - windows fread is very different from Linux fread. Here's implementation of fread as part of glibc library https://github.com/lattera/glibc/blob/master/libio/iofread.c#L30 .

read() calls read system call. https://man7.org/linux/man-pages/man2/syscalls.2.html

what do unbuffered read and buffered read mean?

fread reads data to some intermediate buffer, then copies from that buffer to the memory you passed as argument.

read makes kernel to copy straight to the buffer you passed as argument. There is no intermediate buffer.

why does some say fread() is faster than read() in this topic?

The assumption is that calling system calls is costly - what kernel does inside the system call and system call itself is costly.

Reading the data in a big like 4 kilobyte chunk into an intermediate buffer once and then reading from that buffer in small chunks within your program is faster than context switching to kernel every time to read a small chunks of data so that kernel will do repeatedly a small input/output operation to fetch the data.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aconcagua
Solution 2