1READ(2) Linux Programmer's Manual READ(2)
2
3
4
6 read - read from a file descriptor
7
9 #include <unistd.h>
10
11 ssize_t read(int fd, void *buf, size_t count);
12
14 read() attempts to read up to count bytes from file descriptor fd into
15 the buffer starting at buf.
16
17 On files that support seeking, the read operation commences at the file
18 offset, and the file offset is incremented by the number of bytes read.
19 If the file offset is at or past the end of file, no bytes are read,
20 and read() returns zero.
21
22 If count is zero, read() may detect the errors described below. In the
23 absence of any errors, or if read() does not check for errors, a read()
24 with a count of 0 returns zero and has no other effects.
25
26 According to POSIX.1, if count is greater than SSIZE_MAX, the result is
27 implementation-defined; see NOTES for the upper limit on Linux.
28
30 On success, the number of bytes read is returned (zero indicates end of
31 file), and the file position is advanced by this number. It is not an
32 error if this number is smaller than the number of bytes requested;
33 this may happen for example because fewer bytes are actually available
34 right now (maybe because we were close to end-of-file, or because we
35 are reading from a pipe, or from a terminal), or because read() was
36 interrupted by a signal. See also NOTES.
37
38 On error, -1 is returned, and errno is set appropriately. In this
39 case, it is left unspecified whether the file position (if any)
40 changes.
41
43 EAGAIN The file descriptor fd refers to a file other than a socket and
44 has been marked nonblocking (O_NONBLOCK), and the read would
45 block. See open(2) for further details on the O_NONBLOCK flag.
46
47 EAGAIN or EWOULDBLOCK
48 The file descriptor fd refers to a socket and has been marked
49 nonblocking (O_NONBLOCK), and the read would block.
50 POSIX.1-2001 allows either error to be returned for this case,
51 and does not require these constants to have the same value, so
52 a portable application should check for both possibilities.
53
54 EBADF fd is not a valid file descriptor or is not open for reading.
55
56 EFAULT buf is outside your accessible address space.
57
58 EINTR The call was interrupted by a signal before any data was read;
59 see signal(7).
60
61 EINVAL fd is attached to an object which is unsuitable for reading; or
62 the file was opened with the O_DIRECT flag, and either the
63 address specified in buf, the value specified in count, or the
64 file offset is not suitably aligned.
65
66 EINVAL fd was created via a call to timerfd_create(2) and the wrong
67 size buffer was given to read(); see timerfd_create(2) for fur‐
68 ther information.
69
70 EIO I/O error. This will happen for example when the process is in
71 a background process group, tries to read from its controlling
72 terminal, and either it is ignoring or blocking SIGTTIN or its
73 process group is orphaned. It may also occur when there is a
74 low-level I/O error while reading from a disk or tape. A fur‐
75 ther possible cause of EIO on networked filesystems is when an
76 advisory lock had been taken out on the file descriptor and this
77 lock has been lost. See the Lost locks section of fcntl(2) for
78 further details.
79
80 EISDIR fd refers to a directory.
81
82 Other errors may occur, depending on the object connected to fd.
83
85 SVr4, 4.3BSD, POSIX.1-2001.
86
88 The types size_t and ssize_t are, respectively, unsigned and signed
89 integer data types specified by POSIX.1.
90
91 On Linux, read() (and similar system calls) will transfer at most
92 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actu‐
93 ally transferred. (This is true on both 32-bit and 64-bit systems.)
94
95 On NFS filesystems, reading small amounts of data will update the time‐
96 stamp only the first time, subsequent calls may not do so. This is
97 caused by client side attribute caching, because most if not all NFS
98 clients leave st_atime (last file access time) updates to the server,
99 and client side reads satisfied from the client's cache will not cause
100 st_atime updates on the server as there are no server-side reads. UNIX
101 semantics can be obtained by disabling client-side attribute caching,
102 but in most situations this will substantially increase server load and
103 decrease performance.
104
106 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7 ("Thread Interactions
107 with Regular File Operations"):
108
109 All of the following functions shall be atomic with respect to each
110 other in the effects specified in POSIX.1-2008 when they operate on
111 regular files or symbolic links: ...
112
113 Among the APIs subsequently listed are read() and readv(2). And among
114 the effects that should be atomic across threads (and processes) are
115 updates of the file offset. However, on Linux before version 3.14,
116 this was not the case: if two processes that share an open file
117 description (see open(2)) perform a read() (or readv(2)) at the same
118 time, then the I/O operations were not atomic with respect updating the
119 file offset, with the result that the reads in the two processes might
120 (incorrectly) overlap in the blocks of data that they obtained. This
121 problem was fixed in Linux 3.14.
122
124 close(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2),
125 readlink(2), readv(2), select(2), write(2), fread(3)
126
128 This page is part of release 4.15 of the Linux man-pages project. A
129 description of the project, information about reporting bugs, and the
130 latest version of this page, can be found at
131 https://www.kernel.org/doc/man-pages/.
132
133
134
135Linux 2018-02-02 READ(2)