Home arrow BrainDump arrow Page 4 - Using mmap() for Advanced File I/O

Synchronizing a File with a Mapping - BrainDump

In this fourth part of a seven-part series on Linux I/O file system calls, you will learn how to use mmap(). 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. Using mmap() for Advanced File I/O
  2. Resizing a Mapping
  3. Changing the Protection of a Mapping
  4. Synchronizing a File with a Mapping
  5. Giving Advice on a Mapping
By: O'Reilly Media
Rating: starstarstarstarstar / 5
December 18, 2008

print this article



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 forlenbytes is synchronized to disk. Theaddrargument must be page-aligned; it is generally the return value from a previousmmap()invocation.

Without invocation ofmsync(), 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 ofwrite(), 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.

Theflagsparameter controls the behavior of the synchronizing operation. It is a bitwise OR of the following values:


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


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.


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

EitherMS_ASYNCorMS_SYNCmust 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 setserrnoappropriately. The following are validerrnovalues:


Theflagsparameter has bothMS_SYNCandMS_ASYNCset, a bit other than one of the three valid flags is set, oraddris not page-aligned.


The given memory region (or part of it) is not mapped. Note that Linux will returnENOMEM, 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()returnedEFAULTin place ofENOMEM.

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