BrainDump
  Home arrow BrainDump arrow Page 4 - Advanced File I/O
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
BRAINDUMP

Advanced File I/O
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-11-20


    Table of Contents:
  • Advanced File I/O
  • Scatter/Gather I/O
  • Return values
  • readv() example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Advanced File I/O - readv() example
    ( Page 4 of 4 )

    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 exam ple 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.

    Implementation

    A naïve implementation of readv() and writev() 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;
                              break;
                     
    }
                      ret += nr;

              }

              return ret;
      }

    Thankfully, this is not the Linux implementation: Linux implements readv() and writev() as system calls, and internally performs scatter/gather I/O. In fact, all I/O inside the Linux kernel is vectored; read() and write() 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
     

       

    BRAINDUMP ARTICLES

    - Migrating Oracle to PostgreSQL with Enterpri...
    - Demystifying SELinux on Kernel 2.6
    - Yahoo and Microsoft Create Ad Partnership
    - The Advantages of Obscure Open Source Browse...
    - Dell Announces CSI-style Digital Forensics S...
    - Milepost GCC Speeds Open-Source Development
    - Learn These 10 Programming Languages
    - Tomcat Capacity Planning
    - Internal and External Performance Tuning wit...
    - Tomcat Benchmark Procedure
    - Benchmarking Tomcat Performance
    - Tomcat Performance Tuning
    - Wubi: Windows-based Ubuntu Installer
    - Configuring and Optimizing Your I/O Scheduler
    - Linux I/O Schedulers





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek