1read(2)                       System Calls Manual                      read(2)
2
3
4

NAME

6       read - read from a file descriptor
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       ssize_t read(int fd, void buf[.count], size_t count);
15

DESCRIPTION

17       read()  attempts to read up to count bytes from file descriptor fd into
18       the buffer starting at buf.
19
20       On files that support seeking, the read operation commences at the file
21       offset, and the file offset is incremented by the number of bytes read.
22       If the file offset is at or past the end of file, no  bytes  are  read,
23       and read() returns zero.
24
25       If count is zero, read() may detect the errors described below.  In the
26       absence of any errors, or if read() does not check for errors, a read()
27       with a count of 0 returns zero and has no other effects.
28
29       According to POSIX.1, if count is greater than SSIZE_MAX, the result is
30       implementation-defined; see NOTES for the upper limit on Linux.
31

RETURN VALUE

33       On success, the number of bytes read is returned (zero indicates end of
34       file),  and the file position is advanced by this number.  It is not an
35       error if this number is smaller than the  number  of  bytes  requested;
36       this  may happen for example because fewer bytes are actually available
37       right now (maybe because we were close to end-of-file,  or  because  we
38       are reading from a pipe, or from a terminal), or because read() was in‐
39       terrupted by a signal.  See also NOTES.
40
41       On error, -1 is returned, and errno is set to indicate the  error.   In
42       this  case,  it  is left unspecified whether the file position (if any)
43       changes.
44

ERRORS

46       EAGAIN The file descriptor fd refers to a file other than a socket  and
47              has  been  marked  nonblocking  (O_NONBLOCK), and the read would
48              block.  See open(2) for further details on the O_NONBLOCK flag.
49
50       EAGAIN or EWOULDBLOCK
51              The file descriptor fd refers to a socket and  has  been  marked
52              nonblocking    (O_NONBLOCK),   and   the   read   would   block.
53              POSIX.1-2001 allows either error to be returned for  this  case,
54              and  does not require these constants to have the same value, so
55              a portable application should check for both possibilities.
56
57       EBADF  fd is not a valid file descriptor or is not open for reading.
58
59       EFAULT buf is outside your accessible address space.
60
61       EINTR  The call was interrupted by a signal before any data  was  read;
62              see signal(7).
63
64       EINVAL fd  is attached to an object which is unsuitable for reading; or
65              the file was opened with the O_DIRECT flag, and either  the  ad‐
66              dress  specified  in  buf,  the value specified in count, or the
67              file offset is not suitably aligned.
68
69       EINVAL fd was created via a call to  timerfd_create(2)  and  the  wrong
70              size  buffer was given to read(); see timerfd_create(2) for fur‐
71              ther information.
72
73       EIO    I/O error.  This will happen for example when the process is  in
74              a  background  process group, tries to read from its controlling
75              terminal, and either it is ignoring or blocking SIGTTIN  or  its
76              process  group  is  orphaned.  It may also occur when there is a
77              low-level I/O error while reading from a disk or tape.   A  fur‐
78              ther  possible  cause of EIO on networked filesystems is when an
79              advisory lock had been taken out on the file descriptor and this
80              lock  has been lost.  See the Lost locks section of fcntl(2) for
81              further details.
82
83       EISDIR fd refers to a directory.
84
85       Other errors may occur, depending on the object connected to fd.
86

STANDARDS

88       POSIX.1-2008.
89

HISTORY

91       SVr4, 4.3BSD, POSIX.1-2001.
92

NOTES

94       On Linux, read() (and similar  system  calls)  will  transfer  at  most
95       0x7ffff000  (2,147,479,552)  bytes, returning the number of bytes actu‐
96       ally transferred.  (This is true on both 32-bit and 64-bit systems.)
97
98       On NFS filesystems, reading small amounts of data will update the time‐
99       stamp  only  the  first  time, subsequent calls may not do so.  This is
100       caused by client side attribute caching, because most if  not  all  NFS
101       clients  leave  st_atime (last file access time) updates to the server,
102       and client side reads satisfied from the client's cache will not  cause
103       st_atime updates on the server as there are no server-side reads.  UNIX
104       semantics can be obtained by disabling client-side  attribute  caching,
105       but in most situations this will substantially increase server load and
106       decrease performance.
107

BUGS

109       According to POSIX.1-2008/SUSv4 Section XSI 2.9.7 ("Thread Interactions
110       with Regular File Operations"):
111
112           All of the following functions shall be atomic with respect to each
113           other in the effects specified in POSIX.1-2008 when they operate on
114           regular files or symbolic links: ...
115
116       Among  the APIs subsequently listed are read() and readv(2).  And among
117       the effects that should be atomic across threads  (and  processes)  are
118       updates  of  the file offset.  However, before Linux 3.14, this was not
119       the case: if two processes that share an  open  file  description  (see
120       open(2))  perform a read() (or readv(2)) at the same time, then the I/O
121       operations were not atomic with respect updating the file offset,  with
122       the  result  that  the  reads  in the two processes might (incorrectly)
123       overlap in the blocks of data that they  obtained.   This  problem  was
124       fixed in Linux 3.14.
125

SEE ALSO

127       close(2),  fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2),
128       readlink(2), readv(2), select(2), write(2), fread(3)
129
130
131
132Linux man-pages 6.05              2023-04-03                           read(2)
Impressum