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

readv() example - 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



Now, letís consider an example program that uses the readv() system call to read from the previously generated text file using vectored I/O. This self-contained example is likewise simple yet complete:

  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <sys/uio.h>

  int main ()
          char foo[48], bar[51], baz[49];
          struct iovec iov[3];
          ssize_t nr;
          int fd, i;

          fd = open ("buccaneer.txt", O_RDONLY);
if (fd == -1) {
                  perror ("open");
                  return 1;

          /* set up our iovec structures */
          iov[0].iov_base = foo;
          iov[0].iov_len = sizeof (foo);
          iov[1].iov_base = bar;
          iov[1].iov_len = sizeof (bar);
          iov[2].iov_base = baz;
          iov[2].iov_len = sizeof (baz);

          /* read into the structures with a single call */
          nr = readv (fd, iov, 3);
          if (nr == -1) {
perror ("readv");
                  return 1;

          for (i = 0; i < 3; i++)
                  printf ("%d: %s", i, (char *) iov[i].iov_base);

          if (close (fd)) {
                  perror ("close");
                  return 1;

          return 0;

Running this program after running the previous program produces the following results:

  $ ./readv
0: The term buccaneer comes from the word boucan.
1: A boucan is a wooden frame used for cooking meat.
2: Buccaneer is the West Indies name for a pirate.


A naÔve implementation of readv() andwritev()could be done in user space as a simple loop, something similar to the following:

  #include <unistd.h>
  #include <sys/uio.h>

  ssize_t naive_writev (int fd, const struct iovec *iov, int count)
          ssize_t ret = 0;
          int i;

          for (i = 0; i < count; i++) {
                  ssize_t nr;

                  nr = write (fd, iov[i].iov_base, iov[i].iov_len);
if (nr == -1) {
                          ret = -1;
                  ret += nr;


          return ret;

Thankfully, this is not the Linux implementation: Linux implementsreadv()andwritev()as system calls, and internally performs scatter/gather I/O. In fact, all I/O inside the Linux kernel is vectored;read()andwrite()are implemented as vectored I/O with a vector of only one segment.

Please check back next week for the continuation of this article.

>>> 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: