函数“read”的读取限制是多少?在“unistd.h”中?

发布于 2024-12-03 11:37:05 字数 131 浏览 2 评论 0原文

标准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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寒冷纷飞旳雪 2024-12-10 11:37:05

来自 man read(2)

read() 尝试从文件描述符 fd 中读取最多 count 个字节
从 buf 开始的缓冲区。

如果 count 为零,read() 返回零并且没有其他结果。 如果
count 大于 SSIZE_MAX,结果未指定。

SSIZE_MAX 的值取决于您的系统,但通常它类似于 的最大值>signed long,通常为 231(32 位系统)或 263(64 位系统)。

231 字节是 2 GB,所以您可能是安全的;实际上,实际的设备驱动程序/缓冲区/网络 I/O 永远不会一次性为您提供 2 GB 的数据块。

From man read(2):

read() attempts to read up to count bytes from file descriptor fd into
the buffer starting at buf.

If count is zero, read() returns zero and has no other results. If
count is greater than SSIZE_MAX, the result is unspecified.

The value of SSIZE_MAX depends on your system, but generally it's something akin to the maximum value of signed 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.

御守 2024-12-10 11:37:05

引自 IEEE Std 1003.1(又名 POSIX.1)

如果 nbyte 的值大于 {SSIZE_MAX},则结果由实现定义。

因此,您必须在目标平台上检查 man 2 read 。例如,FreeBSD man 在 ERRORS 部分说:

[EINVAL] 值 nbytes 大于 INT_MAX。

quote from IEEE Std 1003.1 (aka POSIX.1)

If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.

So you have to check man 2 read at your target platform. For example, FreeBSD man says in ERRORS part:

[EINVAL] The value nbytes is greater than INT_MAX.

別甾虛僞 2024-12-10 11:37:05

一般来说,它可以读取 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.

萌吟 2024-12-10 11:37:05

read() 接受一个打开的文件描述符、缓冲区的地址和一个
数量,字节数。它尝试将 count 个字节读入
描述符所描述的文件中的缓冲区。重要的是
确保 buf 指向至少 count 个字节的存储!

它可以读取缓冲区可以容纳的尽可能多的数据,限制是 SSIZE_MAX 以及硬件的限制。

read() takes an open file descriptor, the address of a buffer, and a
number, count of bytes. It attempts to read count bytes into the
buffer from the file described by the descriptor. It is important to
assure that buf points to at least count bytes of storage!

It can read as much as your buffer can hold, the limit is SSIZE_MAX and also the limits of your hardware.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文