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

NAME

6       readv,  writev, preadv, pwritev - read or write data into multiple buf‐
7       fers
8

SYNOPSIS

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   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24       preadv(), pwritev(): _BSD_SOURCE
25

DESCRIPTION

27       The readv() system call reads iovcnt buffers from the  file  associated
28       with the file descriptor fd into the buffers described by iov ("scatter
29       input").
30
31       The writev() system call writes iovcnt buffers of data described by iov
32       to the file associated with the file descriptor fd ("gather output").
33
34       The  pointer  iov  points  to  an array of iovec structures, defined in
35       <sys/uio.h> as:
36
37           struct iovec {
38               void  *iov_base;    /* Starting address */
39               size_t iov_len;     /* Number of bytes to transfer */
40           };
41
42       The readv() system call works just like read(2)  except  that  multiple
43       buffers are filled.
44
45       The  writev() system call works just like write(2) except that multiple
46       buffers are written out.
47
48       Buffers are processed in array order.  This  means  that  readv()  com‐
49       pletely fills iov[0] before proceeding to iov[1], and so on.  (If there
50       is insufficient data, then not all buffers pointed to  by  iov  may  be
51       filled.)   Similarly, writev() writes out the entire contents of iov[0]
52       before proceeding to iov[1], and so on.
53
54       The data transfers performed by readv() and writev()  are  atomic:  the
55       data  written  by  writev()  is  written  as a single block that is not
56       intermingled with output  from  writes  in  other  processes  (but  see
57       pipe(7) for an exception); analogously, readv() is guaranteed to read a
58       contiguous block of data from the file, regardless of  read  operations
59       performed  in  other  threads  or  processes that have file descriptors
60       referring to the same open file description (see open(2)).
61
62   preadv() and pwritev()
63       The preadv() system call combines  the  functionality  of  readv()  and
64       pread(2).   It  performs  the  same  task as readv(), but adds a fourth
65       argument, offset, which specifies the file offset at  which  the  input
66       operation is to be performed.
67
68       The  pwritev()  system  call combines the functionality of writev() and
69       pwrite(2).  It performs the same task as writev(), but  adds  a  fourth
70       argument,  offset,  which specifies the file offset at which the output
71       operation is to be performed.
72
73       The file offset is  not  changed  by  these  system  calls.   The  file
74       referred to by fd must be capable of seeking.
75

RETURN VALUE

77       On  success,  readv()  and  preadv()  return  the number of bytes read;
78       writev() and pwritev() return the number of bytes written.   On  error,
79       -1 is returned, and errno is set appropriately.
80

ERRORS

82       The  errors  are  as  given  for  read(2)  and  write(2).  Furthermore,
83       preadv() and pwritev() can also fail for the same reasons as  lseek(2).
84       Additionally, the following error is defined:
85
86       EINVAL The  sum  of the iov_len values overflows an ssize_t value.  Or,
87              the vector count iovcnt is less than zero or  greater  than  the
88              permitted maximum.
89

VERSIONS

91       preadv()  and pwritev() first appeared in Linux 2.6.30; library support
92       was added in glibc 2.10.
93

CONFORMING TO

95       readv(),  writev():  4.4BSD  (these  system  calls  first  appeared  in
96       4.2BSD),  POSIX.1-2001.   Linux  libc5  used  size_t as the type of the
97       iovcnt argument, and int as the return type.
98
99       preadv(), pwritev(): nonstandard, but present also on the modern BSDs.
100

NOTES

102   Linux notes
103       POSIX.1-2001 allows an implementation to place a limit on the number of
104       items  that  can be passed in iov.  An implementation can advertise its
105       limit by defining IOV_MAX in <limits.h> or at run time via  the  return
106       value  from  sysconf(_SC_IOV_MAX).   On  Linux, the limit advertised by
107       these mechanisms is 1024, which is the true kernel limit.  However, the
108       glibc  wrapper  functions  do  some  extra work if they detect that the
109       underlying kernel system call failed because this limit  was  exceeded.
110       In  the case of readv() the wrapper function allocates a temporary buf‐
111       fer large enough for all of the items specified  by  iov,  passes  that
112       buffer  in  a call to read(2), copies data from the buffer to the loca‐
113       tions specified by the iov_base fields of the elements of iov, and then
114       frees the buffer.  The wrapper function for writev() performs the anal‐
115       ogous task using a temporary buffer and a call to write(2).
116

BUGS

118       It is not advisable to mix calls to readv() or writev(), which  operate
119       on  file  descriptors,  with  the functions from the stdio library; the
120       results will be undefined and probably not what you want.
121

EXAMPLE

123       The following code sample demonstrates the use of writev():
124
125           char *str0 = "hello ";
126           char *str1 = "world\n";
127           struct iovec iov[2];
128           ssize_t nwritten;
129
130           iov[0].iov_base = str0;
131           iov[0].iov_len = strlen(str0);
132           iov[1].iov_base = str1;
133           iov[1].iov_len = strlen(str1);
134
135           nwritten = writev(STDOUT_FILENO, iov, 2);
136

SEE ALSO

138       pread(2), read(2), write(2)
139

COLOPHON

141       This page is part of release 3.53 of the Linux  man-pages  project.   A
142       description  of  the project, and information about reporting bugs, can
143       be found at http://www.kernel.org/doc/man-pages/.
144
145
146
147Linux                             2010-11-17                          READV(2)
Impressum