读取,读取部分读取 [英] read, fread partial reads
问题描述
我似乎在文档中找不到有关此信息.
I can't seem to find info about this in the documentation.
read
系统调用文档说,它的读取内容可能少于指定的内容. read
会尝试读取几次吗?
The read
system call documentation says it may read less than specified.
Does read
attempt to read several times?
我知道 fread
是 read
的包装.当我调用 fread
时,是否有可能从流中读取多次直到它变为0或读取指定的字节,还是仅尝试读取一次?
I know that fread
is a wrapper for read
. When I invoke fread
, is it possible that it will read from the stream several times until it gets 0 or reads specified bytes, or will it only attempt to read once?
我正在从内核模块中创建的char设备读取数据,它从数据结构中传输信息并支持部分读取.我有兴趣读取所有数据,直到它返回0.
I am reading from a char device created in my kernel module, it transfers info from a data structure and supports partial reads. I am interested in reading all of the data until it returns 0.
谢谢
推荐答案
read
的一般思想是,只要有可用数据,它就会立即返回¹.从应用程序的角度来看,这就是您可以假设的全部内容.
The general idea of read
is that it returns as soon as some data is available¹. From an application's perspective, that's all you can assume.
如果您要在内核驱动程序中实现 read
回调,则由 read
决定返回一些数据由您自己决定.但是应用程序会期望 read
"调用可能是部分调用,并且如果确实需要一定数量的字节,则应在循环中调用" read
".有些应用程序希望 read
不阻塞,因此,如果有可用数据,则阻塞 read
调用将是一个坏主意.
If you're implementing the read
callback in a kernel driver, it's up to you when read
decides to return some data. But applications will² expect that read
calls may be partial, and they should call read
in a loop if they really need a certain number of bytes. Some applications want read
not to block, so it would be a bad idea to block in a read
call if some data is available.
fread
函数将一直阻塞,直到读取所需的字节数为止,直到到达文件末尾或发生错误为止.它可以通过循环调用 read
来工作.
The fread
function blocks until it's read as many bytes as were requested, until it's reached the end of the file, or until an error occurs. It works by calling read
in a loop.
¹是否以及何时 read
可以返回0个字节超出了此答案的范围.
²或者至少应该如此.越野车应用程序确实存在.
¹ Whether and when read
may return 0 bytes is beyond the scope of this answer.
² Or at least should. Buggy applications do exist.
这篇关于读取,读取部分读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!