purpose/advantage of mmap()

Posted on 2004-09-30
Last Modified: 2010-08-05
       what is the use of mmap()? when is it used? and how is it different from using ordinary file operations(fscanf, fprintf, etc)? Is there any advantage of using it?
I did go through the man pages formmap() but didnt understand the concept exactly?

Question by:dkamdar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Assisted Solution

brettmjohnson earned 50 total points
ID: 12193755
mmap() "maps the file into memory".  

Conceptually, think of allocating a chunk of memory the same size of the file,
reading the entire file into that chunk of memory, thereafter accessing the data
simply by its memory address - without having to seek within the file and read.

Memory mapping a file is not quite so simple and has additional advantages.  
For instance, you don't need to allocate the memory or read the file yourself.  
The system leverages its Virtual Memory mechanisms to only read in the parts
of the file you attempt to access by its mapped memory address.

LVL 22

Assisted Solution

NovaDenizen earned 50 total points
ID: 12193784
mmap() enables you to treat a file like an in-core array.  Say you have a 1MB file and you've mmap'ed it to the address in char *ptr.  You can now do something like this:
ptr[3] = ptr[100047];
And that will cause the following to happen:
1.  The OS reads the disk block containing the byte at offset 3
2.  The OS reads the disk block containing the byte at offset 100047
3.  The program copies the value from from ptr[100047] to ptr[3]
4.  At some time in the future, the OS will write out the modified first block of the file with the modified ptr[3].

mmap() takes advantage of the OS's internal virtual memory system to make a file on the disk look like it is all in a buffer in main memory.  Only the locations that the program reads are read from disk, and only the portions modified are written out to disk.

LVL 22

Accepted Solution

grg99 earned 25 total points
ID: 12193937
mmap() can be, depending how you look at it, a wonderful thing, or a howwible kludge that bites you when you're not looking.

The good parts are:  you can treat a file as if were an array in memory, no need to fseek(), fread(), fwrite() all the time.

The bad parts are:  mmap() and the VM system it uses was designed and tuned for doing virtual-memory style access patterns, not typical file access patterns.  In some pathological cases, like, ulp, sequential file access, the VM code is going to perform very very poorly, as it will tend to toss out of real memory just the blocks you'll be needing next.    The regular file-system code will do the opposite-- it will anticipatorily read ahead if it sees you starting to read sequentially.

So IMHO if you're going to do something that can just as easily be done with fread(), use the right tool;  if you have a light-weight application that just needs to poke a few bytes in a file, then mmap() may not be too bad.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question