BrainDump
  Home arrow BrainDump arrow Page 4 - Using mmap() for 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? 
BRAINDUMP

Using mmap() for Advanced File I/O
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-12-18


    Table of Contents:
  • Using mmap() for Advanced File I/O
  • Resizing a Mapping
  • Changing the Protection of a Mapping
  • Synchronizing a File with a Mapping
  • Giving Advice on a Mapping

  • 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


    Using mmap() for Advanced File I/O - Synchronizing a File with a Mapping
    ( Page 4 of 5 )

     

    POSIX provides a memory-mapped equivalent of the fsync() system call that we discussed in Chapter 2:

      #include <sys/mman.h>

      int msync (void *addr, size_t len, int flags);

    A call to msync() flushes back to disk any changes made to a file mapped via mmap(), synchronizing the mapped file with the mapping. Specifically, the file or subset of a file associated with the mapping starting at memory address addr and continuing for len bytes is synchronized to disk. The addr argument must be page-aligned; it is generally the return value from a previous mmap() invocation.

    Without invocation of msync() , there is no guarantee that a dirty mapping will be written back to disk until the file is unmapped. This is different from the behavior of write() , where a buffer is dirtied as part of the writing process, and queued for writeback to disk. When writing into a memory mapping, the process directly modifies the file’s pages in the kernel’s page cache, without kernel involvement. The kernel may not synchronize the page cache and the disk anytime soon.

    The flags parameter controls the behavior of the synchronizing operation. It is a bitwise OR of the following values:

    MS_ASYNC

    Specifies that synchronization should occur asynchronously. The update is scheduled, but the msync() call returns immediately without waiting for the writes to take place.

    MS_INVALIDATE

    Specifies that all other cached copies of the mapping be invalidated. Any future access to any mappings of this file will reflect the newly synchronized on-disk contents.

    MS_SYNC

    Specifies that synchronization should occur synchronously. The msync() call will not return until all pages are written back to disk.

    Either MS_ASYNC or MS_SYNC must be specified, but not both.

    Usage is simple:

      if (msync (addr, len, MS_ASYNC) == -1 )
              perror ("msync");

    This example asynchronously synchronizes (say that 10 times fast) to disk the file mapped in the region [addr,addr+len) .

    On success, msync() returns 0. On failure, the call returns -1 , and sets errno appro priately. The following are valid errno values:

    EINVAL

    The flags parameter has both MS_SYNC and MS_ASYNC set, a bit other than one of the three valid flags is set, or addr is not page-aligned.

    ENOMEM

    The given memory region (or part of it) is not mapped. Note that Linux will return ENOMEM , as POSIX dictates, when asked to synchronize a region that is only partly unmapped, but it will still synchronize any valid mappings in the region.

    Before version 2.4.19 of the Linux kernel, msync() returned EFAULT in place of ENOMEM .



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

       

    BRAINDUMP ARTICLES

    - 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
    - Advising the Linux Kernel on File I/O





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT