man-pages/man2/readv.2.html
2021-03-31 01:06:50 +01:00

634 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of READV</TITLE>
</HEAD><BODY>
<H1>READV</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2018-04-30<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
readv, writev, preadv, pwritev, preadv2, pwritev2 - read or write data into multiple buffers
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/uio.h">sys/uio.h</A>&gt;</B>
<B>ssize_t readv(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>);</B>
<B>ssize_t writev(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>);</B>
<B>ssize_t preadv(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>,</B>
<B> off_t </B><I>offset</I><B>);</B>
<B>ssize_t pwritev(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>,</B>
<B> off_t </B><I>offset</I><B>);</B>
<B>ssize_t preadv2(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>,</B>
<B> off_t </B><I>offset</I><B>, int </B><I>flags</I><B>);</B>
<B>ssize_t pwritev2(int </B><I>fd</I><B>, const struct iovec *</B><I>iov</I><B>, int </B><I>iovcnt</I><B>,</B>
<B> off_t </B><I>offset</I><B>, int </B><I>flags</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>preadv</B>(),
<B>pwritev</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BSD_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>readv</B>()
system call reads
<I>iovcnt</I>
buffers from the file associated with the file descriptor
<I>fd</I>
into the buffers described by
<I>iov</I>
(&quot;scatter input&quot;).
<P>
The
<B>writev</B>()
system call writes
<I>iovcnt</I>
buffers of data described by
<I>iov</I>
to the file associated with the file descriptor
<I>fd</I>
(&quot;gather output&quot;).
<P>
The pointer
<I>iov</I>
points to an array of
<I>iovec</I>
structures,
defined in
<I>&lt;<A HREF="file:///usr/include/sys/uio.h">sys/uio.h</A>&gt;</I>
as:
<P>
struct iovec {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;&nbsp;*iov_base;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Starting&nbsp;address&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;iov_len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;bytes&nbsp;to&nbsp;transfer&nbsp;*/
};
<P>
The
<B>readv</B>()
system call works just like
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
except that multiple buffers are filled.
<P>
The
<B>writev</B>()
system call works just like
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
except that multiple buffers are written out.
<P>
Buffers are processed in array order.
This means that
<B>readv</B>()
completely fills
<I>iov</I>[0]
before proceeding to
<I>iov</I>[1],
and so on.
(If there is insufficient data, then not all buffers pointed to by
<I>iov</I>
may be filled.)
Similarly,
<B>writev</B>()
writes out the entire contents of
<I>iov</I>[0]
before proceeding to
<I>iov</I>[1],
and so on.
<P>
The data transfers performed by
<B>readv</B>()
and
<B>writev</B>()
are atomic: the data written by
<B>writev</B>()
is written as a single block that is not intermingled with output
from writes in other processes (but see
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7)
for an exception);
analogously,
<B>readv</B>()
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
(see
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)).
<A NAME="lbAE">&nbsp;</A>
<H3>preadv() and pwritev()</H3>
The
<B>preadv</B>()
system call combines the functionality of
<B>readv</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+pread">pread</A></B>(2).
It performs the same task as
<B>readv</B>(),
but adds a fourth argument,
<I>offset</I>,
which specifies the file offset at which the input operation
is to be performed.
<P>
The
<B>pwritev</B>()
system call combines the functionality of
<B>writev</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+pwrite">pwrite</A></B>(2).
It performs the same task as
<B>writev</B>(),
but adds a fourth argument,
<I>offset</I>,
which specifies the file offset at which the output operation
is to be performed.
<P>
The file offset is not changed by these system calls.
The file referred to by
<I>fd</I>
must be capable of seeking.
<A NAME="lbAF">&nbsp;</A>
<H3>preadv2() and pwritev2()</H3>
<P>
These system calls are similar to
<B>preadv</B>()
and
<B>pwritev</B>()
calls, but add a fifth argument,
<I>flags</I>,
which modifies the behavior on a per-call basis.
<P>
Unlike
<B>preadv</B>()
and
<B>pwritev</B>(),
if the
<I>offset</I>
argument is -1, then the current file offset is used and updated.
<P>
The
<I>flags</I>
argument contains a bitwise OR of zero or more of the following flags:
<DL COMPACT>
<DT id="1"><B>RWF_DSYNC</B> (since Linux 4.7)
<DD>
Provide a per-write equivalent of the
<B>O_DSYNC</B>
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
flag.
This flag is meaningful only for
<B>pwritev2</B>(),
and its effect applies only to the data range written by the system call.
<DT id="2"><B>RWF_HIPRI</B> (since Linux 4.6)
<DD>
High priority read/write.
Allows block-based filesystems to use polling of the device,
which provides lower latency, but may use additional resources.
(Currently, this feature is usable only on a file descriptor opened using the
<B>O_DIRECT</B>
flag.)
<DT id="3"><B>RWF_SYNC</B> (since Linux 4.7)
<DD>
Provide a per-write equivalent of the
<B>O_SYNC</B>
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
flag.
This flag is meaningful only for
<B>pwritev2</B>(),
and its effect applies only to the data range written by the system call.
<DT id="4"><B>RWF_NOWAIT</B> (since Linux 4.14)
<DD>
Do not wait for data which is not immediately available.
If this flag is specified, the
<B>preadv2</B>()
system call will return instantly if it would have to read data from
the backing storage or wait for a lock.
If some data was successfully read, it will return the number of bytes read.
If no bytes were read, it will return -1 and set
<I>errno</I>
to
<B>EAGAIN</B>.
Currently, this flag is meaningful only for
<B>preadv2</B>().
<DT id="5"><B>RWF_APPEND</B> (since Linux 4.16)
<DD>
Provide a per-write equivalent of the
<B>O_APPEND</B>
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
flag.
This flag is meaningful only for
<B>pwritev2</B>(),
and its effect applies only to the data range written by the system call.
The
<I>offset</I>
argument does not affect the write operation;
the data is always appended to the end of the file.
However, if the
<I>offset</I>
argument is -1, the current file offset is updated.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>readv</B>(),
<B>preadv</B>()
and
<B>preadv2</B>()
return the number of bytes read;
<B>writev</B>(),
<B>pwritev</B>()
and
<B>pwritev2</B>()
return the number of bytes written.
<P>
Note that it is not an error for a successful call to transfer fewer bytes
than requested (see
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)).
<P>
On error, -1 is returned, and <I>errno</I> is set appropriately.
<A NAME="lbAH">&nbsp;</A>
<H2>ERRORS</H2>
The errors are as given for
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2).
Furthermore,
<B>preadv</B>(),
<B>preadv2</B>(),
<B>pwritev</B>(),
and
<B>pwritev2</B>()
can also fail for the same reasons as
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2).
Additionally, the following errors are defined:
<DL COMPACT>
<DT id="6"><B>EINVAL</B>
<DD>
The sum of the
<I>iov_len</I>
values overflows an
<I>ssize_t</I>
value.
<DT id="7"><B>EINVAL</B>
<DD>
The vector count,
<I>iovcnt</I>,
is less than zero or greater than the permitted maximum.
<DT id="8"><B>EOPNOTSUPP</B>
<DD>
An unknown flag is specified in <I>flags</I>.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>VERSIONS</H2>
<B>preadv</B>()
and
<B>pwritev</B>()
first appeared in Linux 2.6.30; library support was added in glibc 2.10.
<P>
<B>preadv2</B>()
and
<B>pwritev2</B>()
first appeared in Linux 4.6.
Library support was added in glibc 2.26.
<A NAME="lbAJ">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>readv</B>(),
<B>writev</B>():
POSIX.1-2001, POSIX.1-2008,
4.4BSD (these system calls first appeared in 4.2BSD).
<P>
<B>preadv</B>(),
<B>pwritev</B>():
nonstandard, but present also on the modern BSDs.
<P>
<B>preadv2</B>(),
<B>pwritev2</B>():
nonstandard Linux extension.
<A NAME="lbAK">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1 allows an implementation to place a limit on
the number of items that can be passed in
<I>iov</I>.
An implementation can advertise its limit by defining
<B>IOV_MAX</B>
in
<I>&lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;</I>
or at run time via the return value from
<I>sysconf(_SC_IOV_MAX)</I>.
On modern Linux systems, the limit is 1024.
Back in Linux 2.0 days, this limit was 16.
<A NAME="lbAL">&nbsp;</A>
<H3>C library/kernel differences</H3>
The raw
<B>preadv</B>()
and
<B>pwritev</B>()
system calls have call signatures that differ slightly from that of the
corresponding GNU C library wrapper functions shown in the SYNOPSIS.
The final argument,
<I>offset</I>,
is unpacked by the wrapper functions into two arguments in the system calls:
<P>
<B> unsigned long </B><I>pos_l</I><B>, unsigned long </B><I>pos</I>
<P>
These arguments contain, respectively, the low order and high order 32 bits of
<I>offset</I>.
<A NAME="lbAM">&nbsp;</A>
<H3>Historical C library/kernel differences</H3>
To deal with the fact that
<B>IOV_MAX</B>
was so low on early versions of Linux,
the glibc wrapper functions for
<B>readv</B>()
and
<B>writev</B>()
did some extra work if they detected that the underlying kernel
system call failed because this limit was exceeded.
In the case of
<B>readv</B>(),
the wrapper function allocated a temporary buffer large enough
for all of the items specified by
<I>iov</I>,
passed that buffer in a call to
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
copied data from the buffer to the locations specified by the
<I>iov_base</I>
fields of the elements of
<I>iov</I>,
and then freed the buffer.
The wrapper function for
<B>writev</B>()
performed the analogous task using a temporary buffer and a call to
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2).
<P>
The need for this extra effort in the glibc wrapper functions
went away with Linux 2.2 and later.
However, glibc continued to provide this behavior until version 2.10.
Starting with glibc version 2.9,
the wrapper functions provide this behavior only if the library detects
that the system is running a Linux kernel older than version 2.6.18
(an arbitrarily selected kernel version).
And since glibc 2.20
(which requires a minimum Linux kernel version of 2.6.32),
the glibc wrapper functions always just directly invoke the system calls.
<A NAME="lbAN">&nbsp;</A>
<H2>EXAMPLE</H2>
The following code sample demonstrates the use of
<B>writev</B>():
<P>
char *str0 = &quot;hello &quot;;
char *str1 = &quot;world\n&quot;;
struct iovec iov[2];
ssize_t nwritten;
<P>
iov[0].iov_base = str0;
iov[0].iov_len = strlen(str0);
iov[1].iov_base = str1;
iov[1].iov_len = strlen(str1);
<P>
nwritten = writev(STDOUT_FILENO, iov, 2);
<A NAME="lbAO">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+pread">pread</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
<A NAME="lbAP">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="9"><A HREF="#lbAB">NAME</A><DD>
<DT id="10"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="11"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="12"><A HREF="#lbAE">preadv() and pwritev()</A><DD>
<DT id="13"><A HREF="#lbAF">preadv2() and pwritev2()</A><DD>
</DL>
<DT id="14"><A HREF="#lbAG">RETURN VALUE</A><DD>
<DT id="15"><A HREF="#lbAH">ERRORS</A><DD>
<DT id="16"><A HREF="#lbAI">VERSIONS</A><DD>
<DT id="17"><A HREF="#lbAJ">CONFORMING TO</A><DD>
<DT id="18"><A HREF="#lbAK">NOTES</A><DD>
<DL>
<DT id="19"><A HREF="#lbAL">C library/kernel differences</A><DD>
<DT id="20"><A HREF="#lbAM">Historical C library/kernel differences</A><DD>
</DL>
<DT id="21"><A HREF="#lbAN">EXAMPLE</A><DD>
<DT id="22"><A HREF="#lbAO">SEE ALSO</A><DD>
<DT id="23"><A HREF="#lbAP">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>