开发者

What is the reading limit of function 'read' in 'unistd.h'?

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 r开发者_C百科ead 1 time?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜