1READV(2) Linux Programmer's Manual READV(2)
2
3
4
6 readv, writev, preadv, pwritev, preadv2, pwritev2 - read or write data
7 into multiple buffers
8
10 #include <sys/uio.h>
11
12 ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
13
14 ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
15
16 ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,
17 off_t offset);
18
19 ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,
20 off_t offset);
21
22 ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
23 off_t offset, int flags);
24
25 ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
26 off_t offset, int flags);
27
28 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
29
30 preadv(), pwritev():
31 Since glibc 2.19:
32 _DEFAULT_SOURCE
33 Glibc 2.19 and earlier:
34 _BSD_SOURCE
35
37 The readv() system call reads iovcnt buffers from the file associated
38 with the file descriptor fd into the buffers described by iov ("scatter
39 input").
40
41 The writev() system call writes iovcnt buffers of data described by iov
42 to the file associated with the file descriptor fd ("gather output").
43
44 The pointer iov points to an array of iovec structures, defined in
45 <sys/uio.h> as:
46
47 struct iovec {
48 void *iov_base; /* Starting address */
49 size_t iov_len; /* Number of bytes to transfer */
50 };
51
52 The readv() system call works just like read(2) except that multiple
53 buffers are filled.
54
55 The writev() system call works just like write(2) except that multiple
56 buffers are written out.
57
58 Buffers are processed in array order. This means that readv() com‐
59 pletely fills iov[0] before proceeding to iov[1], and so on. (If there
60 is insufficient data, then not all buffers pointed to by iov may be
61 filled.) Similarly, writev() writes out the entire contents of iov[0]
62 before proceeding to iov[1], and so on.
63
64 The data transfers performed by readv() and writev() are atomic: the
65 data written by writev() is written as a single block that is not
66 intermingled with output from writes in other processes (but see
67 pipe(7) for an exception); analogously, readv() is guaranteed to read a
68 contiguous block of data from the file, regardless of read operations
69 performed in other threads or processes that have file descriptors
70 referring to the same open file description (see open(2)).
71
72 preadv() and pwritev()
73 The preadv() system call combines the functionality of readv() and
74 pread(2). It performs the same task as readv(), but adds a fourth
75 argument, offset, which specifies the file offset at which the input
76 operation is to be performed.
77
78 The pwritev() system call combines the functionality of writev() and
79 pwrite(2). It performs the same task as writev(), but adds a fourth
80 argument, offset, which specifies the file offset at which the output
81 operation is to be performed.
82
83 The file offset is not changed by these system calls. The file
84 referred to by fd must be capable of seeking.
85
86 preadv2() and pwritev2()
87 These system calls are similar to preadv() and pwritev() calls, but add
88 a fifth argument, flags, which modifies the behavior on a per-call
89 basis.
90
91 Unlike preadv() and pwritev(), if the offset argument is -1, then the
92 current file offset is used and updated.
93
94 The flags argument contains a bitwise OR of zero or more of the follow‐
95 ing flags:
96
97 RWF_DSYNC (since Linux 4.7)
98 Provide a per-write equivalent of the O_DSYNC open(2) flag.
99 This flag is meaningful only for pwritev2(), and its effect
100 applies only to the data range written by the system call.
101
102 RWF_HIPRI (since Linux 4.6)
103 High priority read/write. Allows block-based filesystems to use
104 polling of the device, which provides lower latency, but may use
105 additional resources. (Currently, this feature is usable only
106 on a file descriptor opened using the O_DIRECT flag.)
107
108 RWF_SYNC (since Linux 4.7)
109 Provide a per-write equivalent of the O_SYNC open(2) flag. This
110 flag is meaningful only for pwritev2(), and its effect applies
111 only to the data range written by the system call.
112
113 RWF_NOWAIT (since Linux 4.14)
114 Do not wait for data which is not immediately available. If
115 this flag is specified, the preadv2() system call will return
116 instantly if it would have to read data from the backing storage
117 or wait for a lock. If some data was successfully read, it will
118 return the number of bytes read. If no bytes were read, it will
119 return -1 and set errno to EAGAIN. Currently, this flag is
120 meaningful only for preadv2().
121
122 RWF_APPEND (since Linux 4.16)
123 Provide a per-write equivalent of the O_APPEND open(2) flag.
124 This flag is meaningful only for pwritev2(), and its effect
125 applies only to the data range written by the system call. The
126 offset argument does not affect the write operation; the data is
127 always appended to the end of the file. However, if the offset
128 argument is -1, the current file offset is updated.
129
131 On success, readv(), preadv() and preadv2() return the number of bytes
132 read; writev(), pwritev() and pwritev2() return the number of bytes
133 written.
134
135 Note that it is not an error for a successful call to transfer fewer
136 bytes than requested (see read(2) and write(2)).
137
138 On error, -1 is returned, and errno is set appropriately.
139
141 The errors are as given for read(2) and write(2). Furthermore,
142 preadv(), preadv2(), pwritev(), and pwritev2() can also fail for the
143 same reasons as lseek(2). Additionally, the following errors are
144 defined:
145
146 EINVAL The sum of the iov_len values overflows an ssize_t value.
147
148 EINVAL The vector count, iovcnt, is less than zero or greater than the
149 permitted maximum.
150
151 EOPNOTSUPP
152 An unknown flag is specified in flags.
153
155 preadv() and pwritev() first appeared in Linux 2.6.30; library support
156 was added in glibc 2.10.
157
158 preadv2() and pwritev2() first appeared in Linux 4.6. Library support
159 was added in glibc 2.26.
160
162 readv(), writev(): POSIX.1-2001, POSIX.1-2008, 4.4BSD (these system
163 calls first appeared in 4.2BSD).
164
165 preadv(), pwritev(): nonstandard, but present also on the modern BSDs.
166
167 preadv2(), pwritev2(): nonstandard Linux extension.
168
170 POSIX.1 allows an implementation to place a limit on the number of
171 items that can be passed in iov. An implementation can advertise its
172 limit by defining IOV_MAX in <limits.h> or at run time via the return
173 value from sysconf(_SC_IOV_MAX). On modern Linux systems, the limit is
174 1024. Back in Linux 2.0 days, this limit was 16.
175
176 C library/kernel differences
177 The raw preadv() and pwritev() system calls have call signatures that
178 differ slightly from that of the corresponding GNU C library wrapper
179 functions shown in the SYNOPSIS. The final argument, offset, is
180 unpacked by the wrapper functions into two arguments in the system
181 calls:
182
183 unsigned long pos_l, unsigned long pos
184
185 These arguments contain, respectively, the low order and high order 32
186 bits of offset.
187
188 Historical C library/kernel differences
189 To deal with the fact that IOV_MAX was so low on early versions of
190 Linux, the glibc wrapper functions for readv() and writev() did some
191 extra work if they detected that the underlying kernel system call
192 failed because this limit was exceeded. In the case of readv(), the
193 wrapper function allocated a temporary buffer large enough for all of
194 the items specified by iov, passed that buffer in a call to read(2),
195 copied data from the buffer to the locations specified by the iov_base
196 fields of the elements of iov, and then freed the buffer. The wrapper
197 function for writev() performed the analogous task using a temporary
198 buffer and a call to write(2).
199
200 The need for this extra effort in the glibc wrapper functions went away
201 with Linux 2.2 and later. However, glibc continued to provide this
202 behavior until version 2.10. Starting with glibc version 2.9, the
203 wrapper functions provide this behavior only if the library detects
204 that the system is running a Linux kernel older than version 2.6.18 (an
205 arbitrarily selected kernel version). And since glibc 2.20 (which
206 requires a minimum Linux kernel version of 2.6.32), the glibc wrapper
207 functions always just directly invoke the system calls.
208
210 The following code sample demonstrates the use of writev():
211
212 char *str0 = "hello ";
213 char *str1 = "world\n";
214 struct iovec iov[2];
215 ssize_t nwritten;
216
217 iov[0].iov_base = str0;
218 iov[0].iov_len = strlen(str0);
219 iov[1].iov_base = str1;
220 iov[1].iov_len = strlen(str1);
221
222 nwritten = writev(STDOUT_FILENO, iov, 2);
223
225 pread(2), read(2), write(2)
226
228 This page is part of release 5.07 of the Linux man-pages project. A
229 description of the project, information about reporting bugs, and the
230 latest version of this page, can be found at
231 https://www.kernel.org/doc/man-pages/.
232
233
234
235Linux 2020-06-09 READV(2)