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