readv, writev, preadv, pwritev, preadv2, pwritev2 - read or write data into multiple buffers
#include <sys/uio.h>
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,
off_t offset);
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,
off_t offset);
ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
off_t offset, int flags);
ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
off_t offset, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
The readv() system call reads iovcnt
buffers from the file associated with the file descriptor fd
into the buffers described by iov
("scatter input").
The writev() system call writes iovcnt
buffers of data described by iov
to the file associated with
the file descriptor fd
("gather output").
The pointer iov
points to an array of iovec
structures, described in iovec(3type).
The readv() system call works just like read(2) except that multiple buffers are filled.
The writev() system call works just like write(2) except that multiple buffers are written out.
Buffers are processed in array order. This means that
readv() completely fills iov[0]
before
proceeding to iov[1]
, and so on. (If there is insufficient
data, then not all buffers pointed to by iov
may be filled.)
Similarly, writev() writes out the entire contents of
iov[0]
before proceeding to iov[1]
, and so on.
The data transfers performed by readv() and writev() are atomic: the data written by writev() is written as a single block that is not intermingled with output from writes in other processes; analogously, 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 (see open(2)).
The preadv() system call combines the functionality
of readv() and pread(2). It performs
the same task as readv(), but adds a fourth argument,
offset
, which specifies the file offset at which the input
operation is to be performed.
The pwritev() system call combines the functionality
of writev() and pwrite(2). It performs
the same task as writev(), but adds a fourth argument,
offset
, which specifies the file offset at which the output
operation is to be performed.
The file offset is not changed by these system calls. The file
referred to by fd
must be capable of seeking.
These system calls are similar to preadv() and
pwritev() calls, but add a fifth argument,
flags
, which modifies the behavior on a per-call basis.
Unlike preadv() and pwritev(), if
the offset
argument is -1, then the current file offset is used
and updated.
The flags
argument contains a bitwise OR of zero or more of
the following flags:
Provide a per-write equivalent of the O_DSYNC open(2) flag. This flag is meaningful only for pwritev2(), and its effect applies only to the data range written by the system call.
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 O_DIRECT flag.)
Provide a per-write equivalent of the O_SYNC open(2) flag. This flag is meaningful only for pwritev2(), and its effect applies only to the data range written by the system call.
Do not wait for data which is not immediately available. If this flag
is specified, the preadv2() 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
errno
to EAGAIN (but see
BUGS). Currently, this flag is meaningful only for
preadv2().
Provide a per-write equivalent of the O_APPEND
open(2) flag. This flag is meaningful only for
pwritev2(), and its effect applies only to the data
range written by the system call. The offset
argument does not
affect the write operation; the data is always appended to the end of
the file. However, if the offset
argument is -1, the current
file offset is updated.
On success, readv(), preadv(), and preadv2() return the number of bytes read; writev(), pwritev(), and pwritev2() return the number of bytes written.
Note that it is not an error for a successful call to transfer fewer bytes than requested (see read(2) and write(2)).
On error, -1 is returned, and errno
is set to indicate the
error.
The following code sample demonstrates the use of writev():
char *str0 = "hello ";
char *str1 = "world\n";
ssize_t nwritten;
struct iovec iov[2];
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);