函数“read”的读取限制是多少?在“unistd.h”中?
标准unix C有这个函数:
ssize_t read(int fd, void *buf, size_t count);
但是这个“read”函数可以读取1次的最大字节数是多少?
Standard unix C has this function:
ssize_t read(int fd, void *buf, size_t count);
But what is the maximum bytes that this 'read' function can read 1 time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自
man read(2)
:SSIZE_MAX
的值取决于您的系统,但通常它类似于的最大值>signed long
,通常为 231(32 位系统)或 263(64 位系统)。231 字节是 2 GB,所以您可能是安全的;实际上,实际的设备驱动程序/缓冲区/网络 I/O 永远不会一次性为您提供 2 GB 的数据块。
From
man read(2)
:The value of
SSIZE_MAX
depends on your system, but generally it's something akin to the maximum value ofsigned long
, which is often 231 (32-bit systems) or 263 (64-bit systems).231 bytes is 2 gigabytes, so you're probably safe; in practice, the actual device driver/buffers/network I/O is never going to give you a 2 gigabyte chunk of data in one go.
引自 IEEE Std 1003.1(又名 POSIX.1)
因此,您必须在目标平台上检查
man 2 read
。例如,FreeBSD man 在 ERRORS 部分说:quote from IEEE Std 1003.1 (aka POSIX.1)
So you have to check
man 2 read
at your target platform. For example, FreeBSD man says in ERRORS part:一般来说,它可以读取
buf
中可用的字节数。实际上,底层设备驱动程序(无论是文件系统、网络还是管道)返回的内容将少于您想要的,以防没有更多可用的内容。因此,
read
的特定行为取决于内核中的底层驱动程序。这就是为什么始终检查
read
的返回值并检查实际读取的字节很重要。Generally it can read as many bytes as there are available in
buf
. In reality, the underlying device driver (be it the filesystem or the network, or a pipe), would return less than what you want in case there is nothing more available.So, the particular behaviour of
read
depends on the underlying driver in the kernel.This is why it's important to always check the return value of
read
and examine the actual bytes read.它可以读取缓冲区可以容纳的尽可能多的数据,限制是 SSIZE_MAX 以及硬件的限制。
It can read as much as your buffer can hold, the limit is SSIZE_MAX and also the limits of your hardware.