2007-09-20 06:52:22 +00:00
|
|
|
.\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
|
2008-07-08 14:21:20 +00:00
|
|
|
.\" and Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" License.
|
|
|
|
.\" Modified Sat Jul 24 18:34:44 1993 by Rik Faith (faith@cs.unc.edu)
|
|
|
|
.\" Merged readv.[23], 2002-10-17, aeb
|
2007-04-30 15:32:25 +00:00
|
|
|
.\" 2007-04-30 mtk, A fairly major rewrite to fix errors and
|
|
|
|
.\" add more details.
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2007-05-30 05:40:58 +00:00
|
|
|
.TH READV 2 2002-10-17 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
readv, writev \- read or write data into multiple buffers
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <sys/uio.h>
|
|
|
|
.sp
|
2007-04-30 15:32:25 +00:00
|
|
|
.BI "ssize_t readv(int " fd ", const struct iovec *" iov ", int " iovcnt );
|
2004-11-03 13:51:07 +00:00
|
|
|
.sp
|
2007-04-30 15:32:25 +00:00
|
|
|
.BI "ssize_t writev(int " fd ", const struct iovec *" iov ", int " iovcnt );
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function reads
|
2007-04-30 15:32:25 +00:00
|
|
|
.I iovcnt
|
|
|
|
buffers from the file associated with the file descriptor
|
2004-11-03 13:51:07 +00:00
|
|
|
.I fd
|
2007-04-30 15:32:25 +00:00
|
|
|
into the buffers described by
|
2007-09-20 16:26:31 +00:00
|
|
|
.I iov
|
2007-04-30 15:32:25 +00:00
|
|
|
("scatter input").
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR writev ()
|
2007-04-30 15:32:25 +00:00
|
|
|
function writes
|
|
|
|
.I iovcnt
|
|
|
|
buffers of data described by
|
|
|
|
.I iov
|
2004-11-03 13:51:07 +00:00
|
|
|
to the file associated with the file descriptor
|
2007-09-20 16:26:31 +00:00
|
|
|
.I fd
|
2007-04-30 15:32:25 +00:00
|
|
|
("gather output").
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The pointer
|
2007-04-30 15:32:25 +00:00
|
|
|
.I iov
|
2007-06-21 22:55:04 +00:00
|
|
|
points to an array of
|
2007-04-30 15:32:25 +00:00
|
|
|
.I iovec
|
|
|
|
structures,
|
2004-11-03 13:51:07 +00:00
|
|
|
defined in
|
2005-08-09 17:32:16 +00:00
|
|
|
.I <sys/uio.h>
|
2007-04-30 15:32:25 +00:00
|
|
|
as:
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
.br
|
2007-12-19 06:57:44 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
|
|
|
struct iovec {
|
2007-04-30 15:32:25 +00:00
|
|
|
void *iov_base; /* Starting address */
|
|
|
|
size_t iov_len; /* Number of bytes to transfer */
|
2004-11-03 13:51:07 +00:00
|
|
|
};
|
|
|
|
.fi
|
2007-12-19 06:57:44 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function works just like
|
|
|
|
.BR read (2)
|
|
|
|
except that multiple buffers are filled.
|
|
|
|
.PP
|
|
|
|
The
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR writev ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function works just like
|
|
|
|
.BR write (2)
|
|
|
|
except that multiple buffers are written out.
|
2007-04-30 15:32:25 +00:00
|
|
|
.PP
|
|
|
|
Buffers are processed in array order.
|
|
|
|
This means that
|
|
|
|
.BR readv ()
|
|
|
|
completely fills
|
|
|
|
.IR iov [0]
|
2007-06-21 22:55:04 +00:00
|
|
|
before proceeding to
|
2007-04-30 15:32:25 +00:00
|
|
|
.IR iov [1],
|
|
|
|
and so on.
|
|
|
|
(If there is insufficient data, then not all buffers pointed to by
|
|
|
|
.I iov
|
|
|
|
may be filled.)
|
|
|
|
Similarly,
|
|
|
|
.BR writev ()
|
|
|
|
writes out the entire contents of
|
|
|
|
.IR iov [0]
|
2007-06-21 22:55:04 +00:00
|
|
|
before proceeding to
|
2007-04-30 15:32:25 +00:00
|
|
|
.IR iov [1],
|
|
|
|
and so on.
|
|
|
|
.PP
|
|
|
|
The data transfers performed by
|
|
|
|
.BR readv ()
|
|
|
|
and
|
|
|
|
.BR writev ()
|
|
|
|
are atomic: the data written by
|
|
|
|
.BR writev ()
|
|
|
|
is written as a single block that is not intermingled with output
|
|
|
|
from writes in other processes (but see
|
|
|
|
.BR pipe (7)
|
|
|
|
for an exception);
|
|
|
|
analogously,
|
|
|
|
.BR readv ()
|
|
|
|
is guaranteed to read a contiguous block of data from the file,
|
|
|
|
regardless of read operations performed in other threads or processes
|
|
|
|
that have file descriptors referring to the same open file description
|
2007-06-21 22:55:04 +00:00
|
|
|
(see
|
2007-04-30 15:32:25 +00:00
|
|
|
.BR open (2)).
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
|
|
|
On success, the
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function returns the number of bytes read; the
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR writev ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function returns the number of bytes written.
|
|
|
|
On error, \-1 is returned, and \fIerrno\fP is set appropriately.
|
|
|
|
.SH ERRORS
|
|
|
|
The errors are as given for
|
|
|
|
.BR read (2)
|
|
|
|
and
|
|
|
|
.BR write (2).
|
2005-08-09 17:32:16 +00:00
|
|
|
Additionally the following error is defined:
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The sum of the
|
|
|
|
.I iov_len
|
|
|
|
values overflows an
|
2005-08-09 17:32:16 +00:00
|
|
|
.I ssize_t
|
2007-04-12 22:42:49 +00:00
|
|
|
value.
|
2007-07-18 20:24:30 +00:00
|
|
|
Or, the vector count \fIiovcnt\fP is less than zero or greater than the
|
2005-08-09 17:32:16 +00:00
|
|
|
permitted maximum.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
4.4BSD (the
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR writev ()
|
2006-08-03 13:57:17 +00:00
|
|
|
functions first appeared in 4.2BSD), POSIX.1-2001.
|
2008-07-10 20:53:08 +00:00
|
|
|
Linux libc5 used \fIsize_t\fP as the type of the \fIiovcnt\fP argument,
|
2006-02-09 20:24:53 +00:00
|
|
|
and \fIint\fP as return type for these functions.
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" The readv/writev system calls were buggy before Linux 1.3.40.
|
|
|
|
.\" (Says release.libc.)
|
2007-05-18 10:39:45 +00:00
|
|
|
.SH NOTES
|
|
|
|
.SS Linux Notes
|
2007-04-12 22:42:49 +00:00
|
|
|
POSIX.1-2001 allows an implementation to place a limit on
|
2006-08-03 13:57:17 +00:00
|
|
|
the number of items that can be passed in
|
2007-04-30 15:32:25 +00:00
|
|
|
.IR iov .
|
2005-08-09 17:32:16 +00:00
|
|
|
An implementation can advertise its limit by defining
|
|
|
|
.B IOV_MAX
|
|
|
|
in
|
2007-09-20 16:26:31 +00:00
|
|
|
.I <limits.h>
|
2005-08-09 17:32:16 +00:00
|
|
|
or at run time via the return value from
|
|
|
|
.IR sysconf(_SC_IOV_MAX) .
|
|
|
|
On Linux, the limit advertised by these mechanisms is 1024,
|
|
|
|
which is the true kernel limit.
|
|
|
|
However, the glibc wrapper functions do some extra work if
|
|
|
|
they detect that the underlying kernel system call failed because this
|
2007-04-12 22:42:49 +00:00
|
|
|
limit was exceeded.
|
|
|
|
In the case of
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2007-04-12 22:42:49 +00:00
|
|
|
the wrapper function allocates a temporary buffer large enough
|
2005-08-09 17:32:16 +00:00
|
|
|
for all of the items specified by
|
2007-04-30 15:32:25 +00:00
|
|
|
.IR iov ,
|
2007-04-12 22:42:49 +00:00
|
|
|
passes that buffer in a call to
|
2007-05-11 23:07:02 +00:00
|
|
|
.BR read (2),
|
2005-08-09 17:32:16 +00:00
|
|
|
copies data from the buffer to the locations specified by the
|
|
|
|
.I iov_base
|
2007-04-12 22:42:49 +00:00
|
|
|
fields of the elements of
|
2007-04-30 15:32:25 +00:00
|
|
|
.IR iov ,
|
2005-08-09 17:32:16 +00:00
|
|
|
and then frees the buffer.
|
|
|
|
The wrapper function for
|
|
|
|
.BR writev ()
|
|
|
|
performs the analogous task using a temporary buffer and a call to
|
2007-05-11 23:07:02 +00:00
|
|
|
.BR write (2).
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
|
|
|
It is not advisable to mix calls to functions like
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR readv ()
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2005-08-09 17:32:16 +00:00
|
|
|
.BR writev (),
|
2004-11-03 13:51:07 +00:00
|
|
|
which operate on file descriptors, with the functions from the stdio
|
|
|
|
library; the results will be undefined and probably not what you want.
|
2007-04-30 15:32:25 +00:00
|
|
|
.SH EXAMPLE
|
|
|
|
The following code sample demonstrates the use of
|
|
|
|
.BR writev ():
|
|
|
|
|
2007-12-19 06:57:44 +00:00
|
|
|
.in +4n
|
2007-04-30 15:32:25 +00:00
|
|
|
.nf
|
|
|
|
char *str0 = "hello ";
|
|
|
|
char *str1 = "world\\n";
|
|
|
|
struct iovec iov[2];
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
iov[0].iov_base = str0;
|
|
|
|
iov[0].iov_len = strlen(str0);
|
|
|
|
iov[1].iov_base = str1;
|
|
|
|
iov[1].iov_len = strlen(str1);
|
|
|
|
|
|
|
|
nwritten = writev(STDOUT_FILENO, iov, 2);
|
|
|
|
.fi
|
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR read (2),
|
|
|
|
.BR write (2)
|