简体   繁体   English

read 和 fread 有什么区别?

[英]What's the difference between read and fread?

I have a question about the SO question What is the difference between read() and fread() ?我对 SO 问题有疑问read()fread()有什么区别?

How do read() and fread() work? read()fread()是如何工作的? 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() ?如果fread()是通过调用read() ) 来实现的,为什么有些人在 SO 问题C — fopen() vs open()中说fread()read()快?

What's the difference between read and fread? read 和 fread 有什么区别?

fread is a standardized C programming language function that works with a FILE pointer. fread是一种标准化的 C 编程语言 function 与FILE指针一起使用。

read is a C function available on a POSIX compatible system that works with a file descriptor. read是 C function 在与文件描述符一起使用的 POSIX 兼容系统上可用。

how do read() and fread() work? read() 和 fread() 是如何工作的?

fread calls some unknown system defined API to read from a file. fread调用一些未知的系统定义 API 从文件中读取。 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 .要了解特定fread的工作原理,您必须查看特定系统的fread的特定实现 - windows fread与 Linux fread非常不同。 Here's implementation of fread as part of glibc library https://github.com/lattera/glibc/blob/master/libio/iofread.c#L30 .这是fread作为 glibc 库https://github.com/lattera/glibc/blob/master/libio/iofread.c#L30的一部分的实现。

read() calls read system call. read()调用读取系统调用。 https://man7.org/linux/man-pages/man2/syscalls.2.html 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. fread将数据读取到某个中间缓冲区,然后从该缓冲区复制到您作为参数传递的 memory。

read makes kernel to copy straight to the buffer you passed as argument. read使 kernel 直接复制到您作为参数传递的缓冲区。 There is no intermediate buffer.没有中间缓冲区。

why does some say fread() is faster than read() in this topic?为什么有人说 fread() 在这个主题中比 read() 快?

The assumption is that calling system calls is costly - what kernel does inside the system call and system call itself is costly.假设是调用系统调用是昂贵的 - kernel 在系统调用和系统调用本身是昂贵的。

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.一次将 4 KB 大块中的数据读取到中间缓冲区中,然后在程序中以小块的形式从该缓冲区读取比每次读取一小块数据的上下文切换到 kernel 更快,这样 kernel 将重复执行小的输入/输出操作来获取数据。

fread cannot get faster than read provided (!) you use the same buffer size for read as fread does internally. fread不能read提供的更快(!)您使用与fread内部相同的缓冲区大小进行read

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.如果您读取小块数据,那么每次read都直接访问磁盘,因此您会变慢,而相比之下,只要还有数据, fread就会从其缓冲区中获利 - 并且在消耗完下一个大块时,会将下一个大块读入缓冲区立即再次提供从再次调用开始的小块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM