1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
28 .\" <michael@cantor.informatik.rwth-aachen.de>
29 .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
31 .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
33 .\" <michael@cantor.informatik.rwth-aachen.de>
35 .TH READ 2 2014-05-04 "Linux" "Linux Programmer's Manual"
37 read \- read from a file descriptor
40 .B #include <unistd.h>
42 .BI "ssize_t read(int " fd ", void *" buf ", size_t " count );
46 attempts to read up to
48 bytes from file descriptor
50 into the buffer starting at
53 On files that support seeking,
54 the read operation commences at the current file offset,
55 and the file offset is incremented by the number of bytes read.
56 If the current file offset is at or past the end of file,
57 no bytes are read, and
66 detect the errors described below.
67 In the absence of any errors,
70 does not check for errors, a
74 of 0 returns zero and has no other effects.
80 the result is unspecified.
82 On success, the number of bytes read is returned (zero indicates end of
83 file), and the file position is advanced by this number.
84 It is not an error if this number is smaller than the number of bytes
85 requested; this may happen for example because fewer bytes are actually
86 available right now (maybe because we were close to end-of-file, or
87 because we are reading from a pipe, or from a terminal), or because
89 was interrupted by a signal.
90 On error, \-1 is returned, and
93 In this case, it is left unspecified whether
94 the file position (if any) changes.
100 refers to a file other than a socket and has been marked nonblocking
102 and the read would block.
105 for further details on the
109 .BR EAGAIN " or " EWOULDBLOCK
110 .\" Actually EAGAIN on Linux
113 refers to a socket and has been marked nonblocking
115 and the read would block.
116 POSIX.1-2001 allows either error to be returned for this case,
117 and does not require these constants to have the same value,
118 so a portable application should check for both possibilities.
122 is not a valid file descriptor or is not open for reading.
126 is outside your accessible address space.
129 The call was interrupted by a signal before any data was read; see
134 is attached to an object which is unsuitable for reading;
135 or the file was opened with the
137 flag, and either the address specified in
139 the value specified in
141 or the current file offset is not suitably aligned.
145 was created via a call to
146 .BR timerfd_create (2)
147 and the wrong size buffer was given to
150 .BR timerfd_create (2)
151 for further information.
155 This will happen for example when the process is in a
156 background process group, tries to read from its controlling terminal,
157 and either it is ignoring or blocking
161 It may also occur when there is a low-level I/O error
162 while reading from a disk or tape.
166 refers to a directory.
168 Other errors may occur, depending on the object connected to
172 that is interrupted after reading some data
177 or to return the number of bytes already read.
179 SVr4, 4.3BSD, POSIX.1-2001.
181 On NFS filesystems, reading small amounts of data will update the
182 timestamp only the first time, subsequent calls may not do so.
184 by client side attribute caching, because most if not all NFS clients
187 (last file access time)
188 updates to the server, and client side reads satisfied from the
189 client's cache will not cause
191 updates on the server as there are no
193 UNIX semantics can be obtained by disabling client-side attribute caching,
194 but in most situations this will substantially
195 increase server load and decrease performance.
197 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
198 ("Thread Interactions with Regular File Operations"):
201 All of the following functions shall be atomic with respect to
202 each other in the effects specified in POSIX.1-2008 when they
203 operate on regular files or symbolic links: ...
206 Among the APIs subsequently listed are
210 And among the effects that should be atomic across threads (and processes)
211 are updates of the file offset.
212 However, on Linux before version 3.14,
213 this was not the case: if two processes that share
214 an open file description (see
220 at the same time, then the I/O operations were not atomic
221 with respect updating the file offset,
222 with the result that the reads in the two processes
223 might (incorrectly) overlap in the blocks of data that they obtained.
224 This problem was fixed in Linux 3.14.
225 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
226 .\" From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
227 .\" Subject: Update of file offset on write() etc. is non-atomic with I/O
228 .\" Date: 2014-02-17 15:41:37 GMT
229 .\" Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
230 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
231 .\" Author: Linus Torvalds <torvalds@linux-foundation.org>
232 .\" Date: Mon Mar 3 09:36:58 2014 -0800
234 .\" vfs: atomic f_pos accesses as per POSIX