1READV(2)                   Linux Programmer's Manual                  READV(2)
2
3
4

NAME

6       readv,  writev, preadv, pwritev, preadv2, pwritev2 - read or write data
7       into multiple buffers
8

SYNOPSIS

10       #include <sys/uio.h>
11
12       ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
13       ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
14
15       ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,
16                       off_t offset);
17       ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,
18                       off_t offset);
19
20       ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
21                       off_t offset, int flags);
22       ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
23                       off_t offset, int flags);
24
25   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
26
27       preadv(), pwritev():
28           Since glibc 2.19:
29               _DEFAULT_SOURCE
30           Glibc 2.19 and earlier:
31               _BSD_SOURCE
32

DESCRIPTION

34       The readv() system call reads iovcnt buffers from the  file  associated
35       with the file descriptor fd into the buffers described by iov ("scatter
36       input").
37
38       The writev() system call writes iovcnt buffers of data described by iov
39       to the file associated with the file descriptor fd ("gather output").
40
41       The  pointer  iov  points  to  an array of iovec structures, defined in
42       <sys/uio.h> as:
43
44           struct iovec {
45               void  *iov_base;    /* Starting address */
46               size_t iov_len;     /* Number of bytes to transfer */
47           };
48
49       The readv() system call works just like read(2)  except  that  multiple
50       buffers are filled.
51
52       The  writev() system call works just like write(2) except that multiple
53       buffers are written out.
54
55       Buffers are processed in array order.  This  means  that  readv()  com‐
56       pletely fills iov[0] before proceeding to iov[1], and so on.  (If there
57       is insufficient data, then not all buffers pointed to  by  iov  may  be
58       filled.)   Similarly, writev() writes out the entire contents of iov[0]
59       before proceeding to iov[1], and so on.
60
61       The data transfers performed by readv() and writev()  are  atomic:  the
62       data  written  by writev() is written as a single block that is not in‐
63       termingled with output from writes in other processes (but see  pipe(7)
64       for  an  exception);  analogously, readv() is guaranteed to read a con‐
65       tiguous block of data from the file, regardless of read operations per‐
66       formed  in other threads or processes that have file descriptors refer‐
67       ring to the same open file description (see open(2)).
68
69   preadv() and pwritev()
70       The preadv() system call combines  the  functionality  of  readv()  and
71       pread(2).   It performs the same task as readv(), but adds a fourth ar‐
72       gument, offset, which specifies the file offset at which the input  op‐
73       eration is to be performed.
74
75       The  pwritev()  system  call combines the functionality of writev() and
76       pwrite(2).  It performs the same task as writev(), but  adds  a  fourth
77       argument,  offset,  which specifies the file offset at which the output
78       operation is to be performed.
79
80       The file offset is not changed by these system  calls.   The  file  re‐
81       ferred to by fd must be capable of seeking.
82
83   preadv2() and pwritev2()
84       These system calls are similar to preadv() and pwritev() calls, but add
85       a fifth argument, flags, which modifies the behavior on a per-call  ba‐
86       sis.
87
88       Unlike  preadv()  and pwritev(), if the offset argument is -1, then the
89       current file offset is used and updated.
90
91       The flags argument contains a bitwise OR of zero or more of the follow‐
92       ing flags:
93
94       RWF_DSYNC (since Linux 4.7)
95              Provide  a  per-write  equivalent  of  the O_DSYNC open(2) flag.
96              This flag is meaningful only for pwritev2(), and its effect  ap‐
97              plies only to the data range written by the system call.
98
99       RWF_HIPRI (since Linux 4.6)
100              High priority read/write.  Allows block-based filesystems to use
101              polling of the device, which provides lower latency, but may use
102              additional  resources.   (Currently, this feature is usable only
103              on a file descriptor opened using the O_DIRECT flag.)
104
105       RWF_SYNC (since Linux 4.7)
106              Provide a per-write equivalent of the O_SYNC open(2) flag.  This
107              flag  is  meaningful only for pwritev2(), and its effect applies
108              only to the data range written by the system call.
109
110       RWF_NOWAIT (since Linux 4.14)
111              Do not wait for data which is  not  immediately  available.   If
112              this  flag  is  specified, the preadv2() system call will return
113              instantly if it would have to read data from the backing storage
114              or wait for a lock.  If some data was successfully read, it will
115              return the number of bytes read.  If no bytes were read, it will
116              return  -1  and  set  errno  to EAGAIN.  Currently, this flag is
117              meaningful only for preadv2().
118
119       RWF_APPEND (since Linux 4.16)
120              Provide a per-write equivalent of  the  O_APPEND  open(2)  flag.
121              This  flag is meaningful only for pwritev2(), and its effect ap‐
122              plies only to the data range written by the  system  call.   The
123              offset argument does not affect the write operation; the data is
124              always appended to the end of the file.  However, if the  offset
125              argument is -1, the current file offset is updated.
126

RETURN VALUE

128       On success, readv(), preadv(), and preadv2() return the number of bytes
129       read; writev(), pwritev(), and pwritev2() return the  number  of  bytes
130       written.
131
132       Note  that  it  is not an error for a successful call to transfer fewer
133       bytes than requested (see read(2) and write(2)).
134
135       On error, -1 is returned, and errno is set to indicate the error.
136

ERRORS

138       The errors  are  as  given  for  read(2)  and  write(2).   Furthermore,
139       preadv(),  preadv2(),  pwritev(),  and pwritev2() can also fail for the
140       same reasons as lseek(2).  Additionally, the following errors  are  de‐
141       fined:
142
143       EINVAL The sum of the iov_len values overflows an ssize_t value.
144
145       EINVAL The  vector count, iovcnt, is less than zero or greater than the
146              permitted maximum.
147
148       EOPNOTSUPP
149              An unknown flag is specified in flags.
150

VERSIONS

152       preadv() and pwritev() first appeared in Linux 2.6.30; library  support
153       was added in glibc 2.10.
154
155       preadv2()  and pwritev2() first appeared in Linux 4.6.  Library support
156       was added in glibc 2.26.
157

CONFORMING TO

159       readv(), writev(): POSIX.1-2001,  POSIX.1-2008,  4.4BSD  (these  system
160       calls first appeared in 4.2BSD).
161
162       preadv(), pwritev(): nonstandard, but present also on the modern BSDs.
163
164       preadv2(), pwritev2(): nonstandard Linux extension.
165

NOTES

167       POSIX.1  allows  an  implementation  to  place a limit on the number of
168       items that can be passed in iov.  An implementation can  advertise  its
169       limit  by  defining IOV_MAX in <limits.h> or at run time via the return
170       value from sysconf(_SC_IOV_MAX).  On modern Linux systems, the limit is
171       1024.  Back in Linux 2.0 days, this limit was 16.
172
173   C library/kernel differences
174       The  raw  preadv() and pwritev() system calls have call signatures that
175       differ slightly from that of the corresponding GNU  C  library  wrapper
176       functions  shown  in  the SYNOPSIS.  The final argument, offset, is un‐
177       packed by the wrapper functions into two arguments in the system calls:
178
179           unsigned long pos_l, unsigned long pos
180
181       These arguments contain, respectively, the low order and high order  32
182       bits of offset.
183
184   Historical C library/kernel differences
185       To  deal  with  the  fact  that IOV_MAX was so low on early versions of
186       Linux, the glibc wrapper functions for readv() and  writev()  did  some
187       extra  work  if  they  detected  that the underlying kernel system call
188       failed because this limit was exceeded.  In the case  of  readv(),  the
189       wrapper  function  allocated a temporary buffer large enough for all of
190       the items specified by iov, passed that buffer in a  call  to  read(2),
191       copied  data from the buffer to the locations specified by the iov_base
192       fields of the elements of iov, and then freed the buffer.  The  wrapper
193       function  for  writev()  performed the analogous task using a temporary
194       buffer and a call to write(2).
195
196       The need for this extra effort in the glibc wrapper functions went away
197       with Linux 2.2 and later.  However, glibc continued to provide this be‐
198       havior until version 2.10.  Starting with glibc version 2.9, the  wrap‐
199       per  functions  provide  this behavior only if the library detects that
200       the system is running a Linux kernel older than version 2.6.18 (an  ar‐
201       bitrarily  selected  kernel  version).  And since glibc 2.20 (which re‐
202       quires a minimum Linux kernel version of  2.6.32),  the  glibc  wrapper
203       functions always just directly invoke the system calls.
204

EXAMPLES

206       The following code sample demonstrates the use of writev():
207
208           char *str0 = "hello ";
209           char *str1 = "world\n";
210           struct iovec iov[2];
211           ssize_t nwritten;
212
213           iov[0].iov_base = str0;
214           iov[0].iov_len = strlen(str0);
215           iov[1].iov_base = str1;
216           iov[1].iov_len = strlen(str1);
217
218           nwritten = writev(STDOUT_FILENO, iov, 2);
219

SEE ALSO

221       pread(2), read(2), write(2)
222

COLOPHON

224       This  page  is  part of release 5.12 of the Linux man-pages project.  A
225       description of the project, information about reporting bugs,  and  the
226       latest     version     of     this    page,    can    be    found    at
227       https://www.kernel.org/doc/man-pages/.
228
229
230
231Linux                             2021-03-22                          READV(2)
Impressum