Discontiguous data file – the SG – I/O approach

The traditional approach using the {lseek, write} pair of system calls three times in succession worked, of course, but at a rather large performance penalty; the fact is, issuing system calls is considered very expensive. A far superior approach performance-wise is called scatter-gather I/O (SG-I/O, or vectored I/O). The relevant system calls are readv(2) and writev(2); this is their signature:

#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);

These system calls allow you to specify a bunch of segments to read or write in one shot; each segment describes a single I/O operation via a structure called

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.