]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/readv.2
03d37ad6dfed304df1ce790c16c53840a6b5e9c8
[thirdparty/man-pages.git] / man2 / readv.2
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" License.
24 .\" Modified Sat Jul 24 18:34:44 1993 by Rik Faith (faith@cs.unc.edu)
25 .\" Merged readv.[23], 2002-10-17, aeb
26 .\" 2007-04-30 mtk, A fairly major rewrite to fix errors and
27 .\" add more details.
28 .\"
29 .TH READV 2 2002-10-17 "Linux" "Linux Programmer's Manual"
30 .SH NAME
31 readv, writev \- read or write data into multiple buffers
32 .SH SYNOPSIS
33 .nf
34 .B #include <sys/uio.h>
35 .sp
36 .BI "ssize_t readv(int " fd ", const struct iovec *" iov ", int " iovcnt );
37 .sp
38 .BI "ssize_t writev(int " fd ", const struct iovec *" iov ", int " iovcnt );
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR readv ()
43 function reads
44 .I iovcnt
45 buffers from the file associated with the file descriptor
46 .I fd
47 into the buffers described by
48 .IR iov
49 ("scatter input").
50 .PP
51 The
52 .BR writev ()
53 function writes
54 .I iovcnt
55 buffers of data described by
56 .I iov
57 to the file associated with the file descriptor
58 .IR fd
59 ("gather output").
60 .PP
61 The pointer
62 .I iov
63 points to an array of
64 .I iovec
65 structures,
66 defined in
67 .I <sys/uio.h>
68 as:
69 .PP
70 .br
71 .in +0.25in
72 .nf
73 struct iovec {
74 void *iov_base; /* Starting address */
75 size_t iov_len; /* Number of bytes to transfer */
76 };
77 .fi
78 .in 0.25in
79 .PP
80 The
81 .BR readv ()
82 function works just like
83 .BR read (2)
84 except that multiple buffers are filled.
85 .PP
86 The
87 .BR writev ()
88 function works just like
89 .BR write (2)
90 except that multiple buffers are written out.
91 .PP
92 Buffers are processed in array order.
93 This means that
94 .BR readv ()
95 completely fills
96 .IR iov [0]
97 before proceeding to
98 .IR iov [1],
99 and so on.
100 (If there is insufficient data, then not all buffers pointed to by
101 .I iov
102 may be filled.)
103 Similarly,
104 .BR writev ()
105 writes out the entire contents of
106 .IR iov [0]
107 before proceeding to
108 .IR iov [1],
109 and so on.
110 .PP
111 The data transfers performed by
112 .BR readv ()
113 and
114 .BR writev ()
115 are atomic: the data written by
116 .BR writev ()
117 is written as a single block that is not intermingled with output
118 from writes in other processes (but see
119 .BR pipe (7)
120 for an exception);
121 analogously,
122 .BR readv ()
123 is guaranteed to read a contiguous block of data from the file,
124 regardless of read operations performed in other threads or processes
125 that have file descriptors referring to the same open file description
126 (see
127 .BR open (2)).
128 .SH "RETURN VALUE"
129 On success, the
130 .BR readv ()
131 function returns the number of bytes read; the
132 .BR writev ()
133 function returns the number of bytes written.
134 On error, \-1 is returned, and \fIerrno\fP is set appropriately.
135 .SH ERRORS
136 The errors are as given for
137 .BR read (2)
138 and
139 .BR write (2).
140 Additionally the following error is defined:
141 .TP
142 .B EINVAL
143 The sum of the
144 .I iov_len
145 values overflows an
146 .I ssize_t
147 value.
148 Or, the vector count \fIiovcnt\fP is less than zero or greater than the
149 permitted maximum.
150 .SH "CONFORMING TO"
151 4.4BSD (the
152 .BR readv ()
153 and
154 .BR writev ()
155 functions first appeared in 4.2BSD), POSIX.1-2001.
156 Linux libc5 used \fIsize_t\fP as the type of the \fIiovcnt\fP parameter,
157 and \fIint\fP as return type for these functions.
158 .\" The readv/writev system calls were buggy before Linux 1.3.40.
159 .\" (Says release.libc.)
160 .SH NOTES
161 .SS Linux Notes
162 POSIX.1-2001 allows an implementation to place a limit on
163 the number of items that can be passed in
164 .IR iov .
165 An implementation can advertise its limit by defining
166 .B IOV_MAX
167 in
168 .IR <limits.h>
169 or at run time via the return value from
170 .IR sysconf(_SC_IOV_MAX) .
171 On Linux, the limit advertised by these mechanisms is 1024,
172 which is the true kernel limit.
173 However, the glibc wrapper functions do some extra work if
174 they detect that the underlying kernel system call failed because this
175 limit was exceeded.
176 In the case of
177 .BR readv ()
178 the wrapper function allocates a temporary buffer large enough
179 for all of the items specified by
180 .IR iov ,
181 passes that buffer in a call to
182 .BR read (2),
183 copies data from the buffer to the locations specified by the
184 .I iov_base
185 fields of the elements of
186 .IR iov ,
187 and then frees the buffer.
188 The wrapper function for
189 .BR writev ()
190 performs the analogous task using a temporary buffer and a call to
191 .BR write (2).
192 .SH BUGS
193 It is not advisable to mix calls to functions like
194 .BR readv ()
195 or
196 .BR writev (),
197 which operate on file descriptors, with the functions from the stdio
198 library; the results will be undefined and probably not what you want.
199 .SH EXAMPLE
200 The following code sample demonstrates the use of
201 .BR writev ():
202
203 .in +0.5i
204 .nf
205 char *str0 = "hello ";
206 char *str1 = "world\\n";
207 struct iovec iov[2];
208 ssize_t nwritten;
209
210 iov[0].iov_base = str0;
211 iov[0].iov_len = strlen(str0);
212 iov[1].iov_base = str1;
213 iov[1].iov_len = strlen(str1);
214
215 nwritten = writev(STDOUT_FILENO, iov, 2);
216 .fi
217 .in
218 .SH "SEE ALSO"
219 .BR read (2),
220 .BR write (2)