Some applications may need to read or write data to multiple buffers,
which are separated in memory. Although this can be done easily enough
with multiple calls to read and write, it is inefficient
because there is overhead associated with each kernel call.
Instead, many platforms provide special high-speed primitives to perform
these scatter-gather operations in a single kernel call. The GNU C Library
will provide an emulation on any system that lacks these
primitives, so they are not a portability threat. They are defined in
sys/uio.h.
These functions are controlled with arrays of iovec structures,
which describe the location and size of each buffer.
The
iovecstructure describes a buffer. It contains two fields:
void *iov_base- Contains the address of a buffer.
size_t iov_len- Contains the length of the buffer.
Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.
The
readvfunction reads data from filedes and scatters it into the buffers described in vector, which is taken to be count structures long. As each buffer is filled, data is sent to the next.Note that
readvis not guaranteed to fill all the buffers. It may stop at any point, for the same reasonsreadwould.The return value is a count of bytes (not buffers) read, 0 indicating end-of-file, or -1 indicating an error. The possible errors are the same as in
read.
Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.
The
writevfunction gathers data from the buffers described in vector, which is taken to be count structures long, and writes them tofiledes. As each buffer is written, it moves on to the next.Like
readv,writevmay stop midstream under the same conditionswritewould.The return value is a count of bytes written, or -1 indicating an error. The possible errors are the same as in
write.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
readvfunction, with the difference it adds an extra offset parameter of typeoff_tsimilar topread. The data is written to the file starting at position offset. The position of the file descriptor itself is not affected by the operation. The value is the same as before the call.When the source file is compiled with
_FILE_OFFSET_BITS == 64thepreadvfunction is in factpreadv64and the typeoff_thas 64 bits, which makes it possible to handle files up to 2^63 bytes in length.The return value is a count of bytes (not buffers) read, 0 indicating end-of-file, or -1 indicating an error. The possible errors are the same as in
readvandpread.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
preadvfunction with the difference is that the offset parameter is of typeoff64_tinstead ofoff_t. It makes it possible on 32 bit machines to address files larger than 2^31 bytes and up to 2^63 bytes. The file descriptorfiledesmust be opened usingopen64since otherwise the large offsets possible withoff64_twill lead to errors with a descriptor in small file mode.When the source file is compiled using
_FILE_OFFSET_BITS == 64on a 32 bit machine this function is actually available under the namepreadvand so transparently replaces the 32 bit interface.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
writevfunction, with the difference it adds an extra offset parameter of typeoff_tsimilar topwrite. The data is written to the file starting at position offset. The position of the file descriptor itself is not affected by the operation. The value is the same as before the call.However, on Linux, if a file is opened with
O_APPEND,pwriteappends data to the end of the file, regardless of the value ofoffset.When the source file is compiled with
_FILE_OFFSET_BITS == 64thepwritevfunction is in factpwritev64and the typeoff_thas 64 bits, which makes it possible to handle files up to 2^63 bytes in length.The return value is a count of bytes (not buffers) written, 0 indicating end-of-file, or -1 indicating an error. The possible errors are the same as in
writevandpwrite.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
pwritevfunction with the difference is that the offset parameter is of typeoff64_tinstead ofoff_t. It makes it possible on 32 bit machines to address files larger than 2^31 bytes and up to 2^63 bytes. The file descriptorfiledesmust be opened usingopen64since otherwise the large offsets possible withoff64_twill lead to errors with a descriptor in small file mode.When the source file is compiled using
_FILE_OFFSET_BITS == 64on a 32 bit machine this function is actually available under the namepwritevand so transparently replaces the 32 bit interface.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
preadvfunction, with the difference it adds an extra flags parameter of typeint. Additionally, if offset is -1, the current file position is used and updated (like thereadvfunction).The supported flags are dependent of the underlying system. For Linux it supports:
RWF_HIPRI- High priority request. This adds a flag that tells the file system that this is a high priority request for which it is worth to poll the hardware. The flag is purely advisory and can be ignored if not supported. The fd must be opened using
O_DIRECT.RWF_DSYNC- Per-IO synchronization as if the file was opened with
O_DSYNCflag.RWF_SYNC- Per-IO synchronization as if the file was opened with
O_SYNCflag.RWF_NOWAIT- Use nonblocking mode for this operation; that is, this call to
preadv2will fail and seterrnotoEAGAINif the operation would block.RWF_APPEND- Per-IO synchronization as if the file was opened with
O_APPENDflag.When the source file is compiled with
_FILE_OFFSET_BITS == 64thepreadv2function is in factpreadv64v2and the typeoff_thas 64 bits, which makes it possible to handle files up to 2^63 bytes in length.The return value is a count of bytes (not buffers) read, 0 indicating end-of-file, or -1 indicating an error. The possible errors are the same as in
preadvwith the addition of:
EOPNOTSUPP- An unsupported flags was used.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
preadv2function with the difference is that the offset parameter is of typeoff64_tinstead ofoff_t. It makes it possible on 32 bit machines to address files larger than 2^31 bytes and up to 2^63 bytes. The file descriptorfiledesmust be opened usingopen64since otherwise the large offsets possible withoff64_twill lead to errors with a descriptor in small file mode.When the source file is compiled using
_FILE_OFFSET_BITS == 64on a 32 bit machine this function is actually available under the namepreadv2and so transparently replaces the 32 bit interface.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
pwritevfunction, with the difference it adds an extra flags parameter of typeint. Additionally, if offset is -1, the current file position should is used and updated (like thewritevfunction).The supported flags are dependent of the underlying system. For Linux, the supported flags are the same as those for
preadv2.When the source file is compiled with
_FILE_OFFSET_BITS == 64thepwritev2function is in factpwritev64v2and the typeoff_thas 64 bits, which makes it possible to handle files up to 2^63 bytes in length.The return value is a count of bytes (not buffers) write, 0 indicating end-of-file, or -1 indicating an error. The possible errors are the same as in
preadv2.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is similar to the
pwritev2function with the difference is that the offset parameter is of typeoff64_tinstead ofoff_t. It makes it possible on 32 bit machines to address files larger than 2^31 bytes and up to 2^63 bytes. The file descriptorfiledesmust be opened usingopen64since otherwise the large offsets possible withoff64_twill lead to errors with a descriptor in small file mode.When the source file is compiled using
_FILE_OFFSET_BITS == 64on a 32 bit machine this function is actually available under the namepwritev2and so transparently replaces the 32 bit interface.