Scatter/gather I/O is a method of input and output where a single system call writes to a vector of buffers from a single data stream, or, alternatively, reads into a vector of buffers from a single data stream. This type of I/O is so named because the data is scattered into or gathered from the given vector of buffers. An alternative name for this approach to input and output is vectored I/O. In comparison, the standard read and write system calls that we covered in Chapter 2 provide linear I/O. Scatter/gather I/O provides several advantages over linear I/O methods: More natural handling
Efficiency
Performance
Atomicity
Both a more natural I/O method and atomicity are achievable without a scatter/gather I/O mechanism. A process can concatenate the disjoint vectors into a single buffer before writing, and decompose the returned buffer into multiple vectors after reading—that is, a user-space application can perform the scattering and the gathering manually. Such a solution, however, is neither efficient nor fun to implement. readv( ) and writev( )POSIX 1003.1-2001 defines, and Linux implements, a pair of system calls that implement scatter/gather I/O. The Linux implementation satisfies all of the goals listed in the previous section. Thereadv()function readscountsegments from the file descriptorfd into the buffers described byiov: #include <sys/uio.h> ssize_t readv (int fd, The writev()function writes at mostcount segments from the buffers described byiovinto the file descriptorfd: #include <sys/uio.h> ssize_t writev (int fd, The readv() and writev()functions behave the same asread()andwrite(), respectively, except that multiple buffers are read from or written to. Eachiovecstructure describes an independent disjoint buffer, which is called a segment: #include <sys/uio.h> struct iovec { A set of segments is called a vector. Each segment in the vector describes the address and length of a buffer in memory to or from which data should be written or read. The readv()function fills each buffer ofiov_lenbytes completely before proceeding to the next buffer. Thewritev()function always writes out all fulliov_lenbytes before proceeding to the next buffer. Both functions always operate on the segments in order, starting withiov[0], theniov[1], and so on, throughiov[count–1].
blog comments powered by Disqus |
|
|
|
|
|
|
|