Home arrow BrainDump arrow Page 2 - Advanced File I/O

Scatter/Gather I/O - BrainDump

If you're reading to take your understanding of Linux I/O file system calls to the next level, keep reading. This seven-part article series will show you everything from system calls to performance considerations. It is excerpted from chapter four of the book Linux System Programming: Talking Directly to the Kernel and C Library, written by Robert Love (O'Reilly, 2007; ISBN: 0596009585). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Advanced File I/O
  2. Scatter/Gather I/O
  3. Return values
  4. readv() example
By: O'Reilly Media
Rating: starstarstarstarstar / 5
November 20, 2008

print this article



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

If your data is naturally segmented—say, the fields of a predefined header file—vectored I/O allows for intuitive manipulation.


A single vectored I/O operation can replace multiple linear I/O operations.


In addition to a reduction in the number of issued system calls, a vectored I/O implementation can provide improved performance over a linear I/O implementation via internal optimizations.


Unlike with multiple linear I/O operations, a process can execute a single vectored I/O operation with no risk of interleaving of an operation from another process.

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,
                const struct iovec *iov,
int count);

The writev()function writes at mostcount segments from the buffers described byiovinto the file descriptorfd:

  #include <sys/uio.h>

  ssize_t writev (int fd,
                 const struct iovec *iov,
                 int count);

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 {
void *iov_base;   /* pointer to start of buffer */
          size_t iov_len;   /* size of buffer in bytes */

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].

>>> More BrainDump Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates


Dev Shed Tutorial Topics: