read - read from a file descriptor
read - 从一个文件描述符中读
所需头文件
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
read尝试从文件描述符fd中读count字节到缓冲区buf的起始处
If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.
如果count是0,read将返回0,没有其他结果,如果count大于SSZIE_MAX,结果是未定义的
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
成功的时候,返回读到的字节数(EOF代表0),文件偏移量将会增加这个数量,如果读到的字节数小与所要求的数量,这个并不是错误,这是因为实际上并没有那么多字节(可能是因为靠近EOF,或者是因为我们从管道中读,或者是从终端读),或者是因为read被信号终端,错误的时候会返回-1,errno被设置成合适的值,在这种情况下,文件位置是否改变是为定义的
EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.
文件描述符fd涉及一个文件,而不是一个被标记为非阻塞的socket,read会阻塞住
EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
文件描述符fd涉及一个被标记为非阻塞的socket,read会阻塞住,POSIX.1-2001允许这个错误的返回,并且没有要求这些常量具有相同的值,所以一个可移植的程序需要检测两种可能的情况
EBADF fd is not a valid file descriptor or is not open for reading.
fd不是一个合法的文件描述符,或者不是可读的
EFAULT buf is outside your accessible address space.
buf超出了可达的地址空间
EINTR The call was interrupted by a signal before any data was read; see signal(7).
在任何数据被读取到之前调用被信号这哦跟那段
EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.
fd所涉及的文件不适合读操作,或者是文件以O_DIRECT被打开,指定buf的地址,指定count的值,或者是当前文件的偏移量不是相应对齐的
EINVAL fd was created via a call to timerfd_create(2) and the wrong size buffer was given to read(); see timerfd_create(2) for further information.
fd是通过timerfd_create创建的,错误的缓冲区大小用来读
EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling tty, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.
I/O错误,当进程处在后台进程组的时候,尝试从控制仿终端读取数据会发生这个错误,忽略或者阻塞SIGTTIN或者进程组是孤儿的,在从磁盘或者磁带中读的时候,有一个低优先级的I/O错误同样会导致这个错误
EISDIR fd refers to a directory.
fd涉及的是一个路径
Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interrupted after reading some data to return -1 (with errno set to EINTR) or to return the number of bytes already read.
其他可能发生的错误取决于对应的fd,POSIX允许read读到一些数据后被中断,然后返回-1(errno被设置成EINTR),或者返回已经读取到的字节数
On NFS file systems, reading small amounts of data will only update the timestamp the first time, subsequent calls may not do so. This is caused by client side attribute caching, because most if not all NFS clients leave st_atime (last file access time) updates to the server and client side reads satisfied from the client's cache will not cause st_atime updates on the server as there are no server side reads. UNIX semantics can be obtained by disabling client side attribute caching, but in most situations this will substantially increase server load and decrease performance.
在NFS文件系统中,读取少量的数据仅仅只会在开始的时候更新时间戳,后续调用就不再更新了,这是客户端的缓冲机制导致的,因为大多数NFS客户端更新st_atime到服务器,客户端从客户的缓冲区读需要的数据不会导致st_atime更新,这样子服务器就不会有副作用,unix环境可以禁用客户端缓冲副作用,但是在多数情况下,这个实际上会加大服务器的负载和降低性能
Many file systems and disks were considered to be fast enough that the implementation of O_NONBLOCK was deemed unnecessary. So, O_NONBLOCK may not be available on files and/or disks.
许多文件系统和磁盘已经被设计的足够快速了,因此O_NONBLOCK被视作不必要的,因此O_NONBLOCK在文件或者磁盘上可能是不可用的
read - 从一个文件描述符中读
所需头文件
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
read尝试从文件描述符fd中读count字节到缓冲区buf的起始处
If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.
如果count是0,read将返回0,没有其他结果,如果count大于SSZIE_MAX,结果是未定义的
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
成功的时候,返回读到的字节数(EOF代表0),文件偏移量将会增加这个数量,如果读到的字节数小与所要求的数量,这个并不是错误,这是因为实际上并没有那么多字节(可能是因为靠近EOF,或者是因为我们从管道中读,或者是从终端读),或者是因为read被信号终端,错误的时候会返回-1,errno被设置成合适的值,在这种情况下,文件位置是否改变是为定义的
EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.
文件描述符fd涉及一个文件,而不是一个被标记为非阻塞的socket,read会阻塞住
EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
文件描述符fd涉及一个被标记为非阻塞的socket,read会阻塞住,POSIX.1-2001允许这个错误的返回,并且没有要求这些常量具有相同的值,所以一个可移植的程序需要检测两种可能的情况
EBADF fd is not a valid file descriptor or is not open for reading.
fd不是一个合法的文件描述符,或者不是可读的
EFAULT buf is outside your accessible address space.
buf超出了可达的地址空间
EINTR The call was interrupted by a signal before any data was read; see signal(7).
在任何数据被读取到之前调用被信号这哦跟那段
EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.
fd所涉及的文件不适合读操作,或者是文件以O_DIRECT被打开,指定buf的地址,指定count的值,或者是当前文件的偏移量不是相应对齐的
EINVAL fd was created via a call to timerfd_create(2) and the wrong size buffer was given to read(); see timerfd_create(2) for further information.
fd是通过timerfd_create创建的,错误的缓冲区大小用来读
EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling tty, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.
I/O错误,当进程处在后台进程组的时候,尝试从控制仿终端读取数据会发生这个错误,忽略或者阻塞SIGTTIN或者进程组是孤儿的,在从磁盘或者磁带中读的时候,有一个低优先级的I/O错误同样会导致这个错误
EISDIR fd refers to a directory.
fd涉及的是一个路径
Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interrupted after reading some data to return -1 (with errno set to EINTR) or to return the number of bytes already read.
其他可能发生的错误取决于对应的fd,POSIX允许read读到一些数据后被中断,然后返回-1(errno被设置成EINTR),或者返回已经读取到的字节数
On NFS file systems, reading small amounts of data will only update the timestamp the first time, subsequent calls may not do so. This is caused by client side attribute caching, because most if not all NFS clients leave st_atime (last file access time) updates to the server and client side reads satisfied from the client's cache will not cause st_atime updates on the server as there are no server side reads. UNIX semantics can be obtained by disabling client side attribute caching, but in most situations this will substantially increase server load and decrease performance.
在NFS文件系统中,读取少量的数据仅仅只会在开始的时候更新时间戳,后续调用就不再更新了,这是客户端的缓冲机制导致的,因为大多数NFS客户端更新st_atime到服务器,客户端从客户的缓冲区读需要的数据不会导致st_atime更新,这样子服务器就不会有副作用,unix环境可以禁用客户端缓冲副作用,但是在多数情况下,这个实际上会加大服务器的负载和降低性能
Many file systems and disks were considered to be fast enough that the implementation of O_NONBLOCK was deemed unnecessary. So, O_NONBLOCK may not be available on files and/or disks.
许多文件系统和磁盘已经被设计的足够快速了,因此O_NONBLOCK被视作不必要的,因此O_NONBLOCK在文件或者磁盘上可能是不可用的