1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2009-2015 Michael Kerrisk, <mtk.manpages.gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
8 .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
9 .\" <michael@cantor.informatik.rwth-aachen.de>
10 .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
11 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
12 .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
13 .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
14 .\" <michael@cantor.informatik.rwth-aachen.de>
16 .TH read 2 (date) "Linux man-pages (unreleased)"
18 read \- read from a file descriptor
21 .RI ( libc ", " \-lc )
24 .B #include <unistd.h>
26 .BI "ssize_t read(int " fd ", void " buf [. count "], size_t " count );
30 attempts to read up to
32 bytes from file descriptor
34 into the buffer starting at
37 On files that support seeking,
38 the read operation commences at the file offset,
39 and the file offset is incremented by the number of bytes read.
40 If the file offset is at or past the end of file,
41 no bytes are read, and
50 detect the errors described below.
51 In the absence of any errors,
54 does not check for errors, a
58 of 0 returns zero and has no other effects.
60 According to POSIX.1, if
64 the result is implementation-defined;
65 see NOTES for the upper limit on Linux.
67 On success, the number of bytes read is returned (zero indicates end of
68 file), and the file position is advanced by this number.
69 It is not an error if this number is smaller than the number of bytes
70 requested; this may happen for example because fewer bytes are actually
71 available right now (maybe because we were close to end-of-file, or
72 because we are reading from a pipe, or from a terminal), or because
74 was interrupted by a signal.
77 On error, \-1 is returned, and
79 is set to indicate the error.
80 In this case, it is left unspecified whether
81 the file position (if any) changes.
87 refers to a file other than a socket and has been marked nonblocking
89 and the read would block.
92 for further details on the
96 .BR EAGAIN " or " EWOULDBLOCK
97 .\" Actually EAGAIN on Linux
100 refers to a socket and has been marked nonblocking
102 and the read would block.
103 POSIX.1-2001 allows either error to be returned for this case,
104 and does not require these constants to have the same value,
105 so a portable application should check for both possibilities.
109 is not a valid file descriptor or is not open for reading.
113 is outside your accessible address space.
116 The call was interrupted by a signal before any data was read; see
121 is attached to an object which is unsuitable for reading;
122 or the file was opened with the
124 flag, and either the address specified in
126 the value specified in
128 or the file offset is not suitably aligned.
132 was created via a call to
133 .BR timerfd_create (2)
134 and the wrong size buffer was given to
137 .BR timerfd_create (2)
138 for further information.
142 This will happen for example when the process is in a
143 background process group, tries to read from its controlling terminal,
144 and either it is ignoring or blocking
148 It may also occur when there is a low-level I/O error
149 while reading from a disk or tape.
150 A further possible cause of
152 on networked filesystems is when an advisory lock had been taken
153 out on the file descriptor and this lock has been lost.
162 refers to a directory.
164 Other errors may occur, depending on the object connected to
169 SVr4, 4.3BSD, POSIX.1-2001.
173 (and similar system calls) will transfer at most
174 0x7ffff000 (2,147,479,552) bytes,
175 returning the number of bytes actually transferred.
176 .\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
177 (This is true on both 32-bit and 64-bit systems.)
179 On NFS filesystems, reading small amounts of data will update the
180 timestamp only the first time, subsequent calls may not do so.
182 by client side attribute caching, because most if not all NFS clients
185 (last file access time)
186 updates to the server, and client side reads satisfied from the
187 client's cache will not cause
189 updates on the server as there are no
191 UNIX semantics can be obtained by disabling client-side attribute caching,
192 but in most situations this will substantially
193 increase server load and decrease performance.
195 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
196 ("Thread Interactions with Regular File Operations"):
199 All of the following functions shall be atomic with respect to
200 each other in the effects specified in POSIX.1-2008 when they
201 operate on regular files or symbolic links: ...
204 Among the APIs subsequently listed are
208 And among the effects that should be atomic across threads (and processes)
209 are updates of the file offset.
210 However, before Linux 3.14,
211 this was not the case: if two processes that share
212 an open file description (see
218 at the same time, then the I/O operations were not atomic
219 with respect updating the file offset,
220 with the result that the reads in the two processes
221 might (incorrectly) overlap in the blocks of data that they obtained.
222 This problem was fixed in Linux 3.14.
223 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
224 .\" From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
225 .\" Subject: Update of file offset on write() etc. is non-atomic with I/O
226 .\" Date: 2014-02-17 15:41:37 GMT
227 .\" Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
228 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
229 .\" Author: Linus Torvalds <torvalds@linux-foundation.org>
230 .\" Date: Mon Mar 3 09:36:58 2014 -0800
232 .\" vfs: atomic f_pos accesses as per POSIX