1READV(2) Linux Programmer's Manual READV(2)
2
3
4
6 readv, writev - read or write data into multiple buffers
7
9 #include <sys/uio.h>
10
11 ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
12
13 ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
14
16 The readv() function reads iovcnt buffers from the file associated with
17 the file descriptor fd into the buffers described by iov ("scatter
18 input").
19
20 The writev() function writes iovcnt buffers of data described by iov to
21 the file associated with the file descriptor fd ("gather output").
22
23 The pointer iov points to an array of iovec structures, defined in
24 <sys/uio.h> as:
25
26 struct iovec {
27 void *iov_base; /* Starting address */
28 size_t iov_len; /* Number of bytes to transfer */
29 };
30
31 The readv() function works just like read(2) except that multiple buf‐
32 fers are filled.
33
34 The writev() function works just like write(2) except that multiple
35 buffers are written out.
36
37 Buffers are processed in array order. This means that readv() com‐
38 pletely fills iov[0] before proceeding to iov[1], and so on. (If there
39 is insufficient data, then not all buffers pointed to by iov may be
40 filled.) Similarly, writev() writes out the entire contents of iov[0]
41 before proceeding to iov[1], and so on.
42
43 The data transfers performed by readv() and writev() are atomic: the
44 data written by writev() is written as a single block that is not
45 intermingled with output from writes in other processes (but see
46 pipe(7) for an exception); analogously, readv() is guaranteed to read a
47 contiguous block of data from the file, regardless of read operations
48 performed in other threads or processes that have file descriptors
49 referring to the same open file description (see open(2)).
50
52 On success, the readv() function returns the number of bytes read; the
53 writev() function returns the number of bytes written. On error, -1 is
54 returned, and errno is set appropriately.
55
57 The errors are as given for read(2) and write(2). Additionally the
58 following error is defined:
59
60 EINVAL The sum of the iov_len values overflows an ssize_t value. Or,
61 the vector count iovcnt is less than zero or greater than the
62 permitted maximum.
63
65 4.4BSD (the readv() and writev() functions first appeared in 4.2BSD),
66 POSIX.1-2001. Linux libc5 used size_t as the type of the iovcnt argu‐
67 ment, and int as return type for these functions.
68
70 Linux Notes
71 POSIX.1-2001 allows an implementation to place a limit on the number of
72 items that can be passed in iov. An implementation can advertise its
73 limit by defining IOV_MAX in <limits.h> or at run time via the return
74 value from sysconf(_SC_IOV_MAX). On Linux, the limit advertised by
75 these mechanisms is 1024, which is the true kernel limit. However, the
76 glibc wrapper functions do some extra work if they detect that the
77 underlying kernel system call failed because this limit was exceeded.
78 In the case of readv() the wrapper function allocates a temporary buf‐
79 fer large enough for all of the items specified by iov, passes that
80 buffer in a call to read(2), copies data from the buffer to the loca‐
81 tions specified by the iov_base fields of the elements of iov, and then
82 frees the buffer. The wrapper function for writev() performs the anal‐
83 ogous task using a temporary buffer and a call to write(2).
84
86 It is not advisable to mix calls to functions like readv() or writev(),
87 which operate on file descriptors, with the functions from the stdio
88 library; the results will be undefined and probably not what you want.
89
91 The following code sample demonstrates the use of writev():
92
93 char *str0 = "hello ";
94 char *str1 = "world\n";
95 struct iovec iov[2];
96 ssize_t nwritten;
97
98 iov[0].iov_base = str0;
99 iov[0].iov_len = strlen(str0);
100 iov[1].iov_base = str1;
101 iov[1].iov_len = strlen(str1);
102
103 nwritten = writev(STDOUT_FILENO, iov, 2);
104
106 read(2), write(2)
107
109 This page is part of release 3.25 of the Linux man-pages project. A
110 description of the project, and information about reporting bugs, can
111 be found at http://www.kernel.org/doc/man-pages/.
112
113
114
115Linux 2002-10-17 READV(2)