1 .\" Copyright (C) 2007, 2010 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Sat Jul 24 18:34:44 1993 by Rik Faith (faith@cs.unc.edu)
7 .\" Merged readv.[23], 2002-10-17, aeb
8 .\" 2007-04-30 mtk, A fairly major rewrite to fix errors and
10 .\" 2010-11-16, mtk, Added documentation of preadv() and pwritev()
12 .TH READV 2 2022-09-17 "Linux man-pages (unreleased)"
14 readv, writev, preadv, pwritev, preadv2, pwritev2 \-
15 read or write data into multiple buffers
18 .RI ( libc ", " \-lc )
21 .B #include <sys/uio.h>
23 .BI "ssize_t readv(int " fd ", const struct iovec *" iov ", int " iovcnt );
24 .BI "ssize_t writev(int " fd ", const struct iovec *" iov ", int " iovcnt );
26 .BI "ssize_t preadv(int " fd ", const struct iovec *" iov ", int " iovcnt ,
27 .BI " off_t " offset );
28 .BI "ssize_t pwritev(int " fd ", const struct iovec *" iov ", int " iovcnt ,
29 .BI " off_t " offset );
31 .BI "ssize_t preadv2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
32 .BI " off_t " offset ", int " flags );
33 .BI "ssize_t pwritev2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
34 .BI " off_t " offset ", int " flags );
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
47 Glibc 2.19 and earlier:
55 buffers from the file associated with the file descriptor
57 into the buffers described by
65 buffers of data described by
67 to the file associated with the file descriptor
81 system call works just like
83 except that multiple buffers are filled.
87 system call works just like
89 except that multiple buffers are written out.
91 Buffers are processed in array order.
99 (If there is insufficient data, then not all buffers pointed to by
104 writes out the entire contents of
110 The data transfers performed by
114 are atomic: the data written by
115 .\" Regarding atomicity, see https://bugzilla.kernel.org/show_bug.cgi?id=10596
117 is written as a single block that is not intermingled with output
118 from writes in other processes;
121 is guaranteed to read a contiguous block of data from the file,
122 regardless of read operations performed in other threads or processes
123 that have file descriptors referring to the same open file description
126 .SS preadv() and pwritev()
129 system call combines the functionality of
133 It performs the same task as
135 but adds a fourth argument,
137 which specifies the file offset at which the input operation
142 system call combines the functionality of
146 It performs the same task as
148 but adds a fourth argument,
150 which specifies the file offset at which the output operation
153 The file offset is not changed by these system calls.
154 The file referred to by
156 must be capable of seeking.
157 .SS preadv2() and pwritev2()
158 These system calls are similar to
162 calls, but add a fifth argument,
164 which modifies the behavior on a per-call basis.
172 argument is \-1, then the current file offset is used and updated.
176 argument contains a bitwise OR of zero or more of the following flags:
178 .BR RWF_DSYNC " (since Linux 4.7)"
179 .\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
180 Provide a per-write equivalent of the
184 This flag is meaningful only for
186 and its effect applies only to the data range written by the system call.
188 .BR RWF_HIPRI " (since Linux 4.6)"
189 High priority read/write.
190 Allows block-based filesystems to use polling of the device,
191 which provides lower latency, but may use additional resources.
192 (Currently, this feature is usable only on a file descriptor opened using the
196 .BR RWF_SYNC " (since Linux 4.7)"
197 .\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
198 Provide a per-write equivalent of the
202 This flag is meaningful only for
204 and its effect applies only to the data range written by the system call.
206 .BR RWF_NOWAIT " (since Linux 4.14)"
207 .\" commit 3239d834847627b6634a4139cf1dc58f6f137a46
208 .\" commit 91f9943e1c7b6638f27312d03fe71fcc67b23571
209 Do not wait for data which is not immediately available.
210 If this flag is specified, the
212 system call will return instantly if it would have to read data from
213 the backing storage or wait for a lock.
214 If some data was successfully read, it will return the number of bytes read.
215 If no bytes were read, it will return \-1 and set
221 Currently, this flag is meaningful only for
224 .BR RWF_APPEND " (since Linux 4.16)"
225 .\" commit e1fc742e14e01d84d9693c4aca4ab23da65811fb
226 Provide a per-write equivalent of the
230 This flag is meaningful only for
232 and its effect applies only to the data range written by the system call.
235 argument does not affect the write operation;
236 the data is always appended to the end of the file.
239 argument is \-1, the current file offset is updated.
246 return the number of bytes read;
251 return the number of bytes written.
253 Note that it is not an error for a successful call to transfer fewer bytes
259 On error, \-1 is returned, and \fIerrno\fP is set to indicate the error.
261 The errors are as given for
271 can also fail for the same reasons as
273 Additionally, the following errors are defined:
285 is less than zero or greater than the permitted maximum.
288 An unknown flag is specified in \fIflags\fP.
293 first appeared in Linux 2.6.30; library support was added in glibc 2.10.
298 first appeared in Linux 4.6.
299 Library support was added in glibc 2.26.
303 POSIX.1-2001, POSIX.1-2008,
304 4.4BSD (these system calls first appeared in 4.2BSD).
305 .\" Linux libc5 used \fIsize_t\fP as the type of the \fIiovcnt\fP argument,
306 .\" and \fIint\fP as the return type.
307 .\" The readv/writev system calls were buggy before Linux 1.3.40.
308 .\" (Says release.libc.)
312 nonstandard, but present also on the modern BSDs.
316 nonstandard Linux extension.
318 POSIX.1 allows an implementation to place a limit on
319 the number of items that can be passed in
321 An implementation can advertise its limit by defining
325 or at run time via the return value from
326 .IR sysconf(_SC_IOV_MAX) .
327 On modern Linux systems, the limit is 1024.
328 Back in Linux 2.0 days, this limit was 16.
331 .SS C library/kernel differences
336 system calls have call signatures that differ slightly from that of the
337 corresponding GNU C library wrapper functions shown in the SYNOPSIS.
340 is unpacked by the wrapper functions into two arguments in the system calls:
342 .BI " unsigned long " pos_l ", unsigned long " pos
344 These arguments contain, respectively, the low order and high order 32 bits of
346 .SS Historical C library/kernel differences
347 To deal with the fact that
349 was so low on early versions of Linux,
350 the glibc wrapper functions for
354 did some extra work if they detected that the underlying kernel
355 system call failed because this limit was exceeded.
358 the wrapper function allocated a temporary buffer large enough
359 for all of the items specified by
361 passed that buffer in a call to
363 copied data from the buffer to the locations specified by the
365 fields of the elements of
367 and then freed the buffer.
368 The wrapper function for
370 performed the analogous task using a temporary buffer and a call to
373 The need for this extra effort in the glibc wrapper functions
374 went away with Linux 2.2 and later.
375 However, glibc continued to provide this behavior until version 2.10.
376 Starting with glibc version 2.9,
377 the wrapper functions provide this behavior only if the library detects
378 that the system is running a Linux kernel older than version 2.6.18
379 (an arbitrarily selected kernel version).
381 (which requires a minimum Linux kernel version of 2.6.32),
382 the glibc wrapper functions always just directly invoke the system calls.
384 Linux 5.9 and 5.10 have a bug where
388 flag may return 0 even when not at end of file.
390 .\" <https://lore.kernel.org/linux-fsdevel/fea8b16d-5a69-40f9-b123-e84dcd6e8f2e@www.fastmail.com/T/#u>
391 .\" The bug was introduced in
392 .\" efa8480a831 fs: RWF_NOWAIT should imply IOCB_NOIO
394 .\" 06c0444290 mm/filemap.c: generic_file_buffered_read() now uses find_get_pages_contig
396 The following code sample demonstrates the use of
401 char *str0 = "hello ";
402 char *str1 = "world\en";
406 iov[0].iov_base = str0;
407 iov[0].iov_len = strlen(str0);
408 iov[1].iov_base = str1;
409 iov[1].iov_len = strlen(str1);
411 nwritten = writev(STDOUT_FILENO, iov, 2);