Vectored I/O
Appearance
In
vector
of buffers. Scatter/gather refers to the process of gathering data from, or scattering data into, the given set of buffers. Vectored I/O can operate synchronously or asynchronously. The main reasons for using vectored I/O are efficiency and convenience.
Vectored I/O has several potential uses:
- processmight perform I/O on the same file between the first process' reads or writes, thereby corrupting the file or compromising the integrity of the input
- Concatenating output: an application that wants to write non-sequentially placed data in memory can do so in one vectored I/O operation. For example, writing a fixed-size header and its associated payload data that are placed non-sequentially in memory can be done by a single vectored I/O operation without first concatenating the header and the payload to another buffer
- Efficiency: one vectored I/O read or write can replace many ordinary reads or writes, and thus save on the overhead involved in syscalls
- Splitting input: when reading data held in a format that defines a fixed-size header, one can use a vector of buffers in which the first buffer is the size of that header; and the second buffer will contain the data associated with the header
Standards bodies document the applicable functions readv
provides separate WSASend
and WSARecv
functions without this requirement.
While working directly with a vector of buffers can be significantly harder than working with a single buffer, using higher-level APIs[4] for working efficiently can mitigate the difficulties.
Examples
The following example in the C programming language prints "Hello, Wikipedia Community!" to the
standard output
. Each word is saved into a single buffer and with only one call to writev()
, all buffers are printed to the standard output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
int main(int argc, char *argv[])
{
const char buf1[] = "Hello, ";
const char buf2[] = "Wikipedia ";
const char buf3[] = "Community!\n";
struct iovec bufs[] = {
{ .iov_base = (void *)buf1, .iov_len = strlen(buf1) },
{ .iov_base = (void *)buf2, .iov_len = strlen(buf2) },
{ .iov_base = (void *)buf3, .iov_len = strlen(buf3) },
};
if (writev(STDOUT_FILENO, bufs, sizeof(bufs) / sizeof(bufs[0])) == -1)
{
perror("writev()");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
See also
- Zero copy
References
- ^ readv Archived 2008-09-05 at the Wayback Machine in the Single Unix Specification
- ^ writev Archived 2007-12-17 at the Wayback Machine in the Single Unix Specification
- ^ ReadFileScatter in MSDN Library
- ^ Vstr Archived 2017-03-05 at the Wayback Machine the Vectored String API