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

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


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.


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.


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.