purpose/advantage of mmap()

Posted on 2004-09-30
Medium Priority
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
LVL 23

Assisted Solution

brettmjohnson earned 200 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 200 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 100 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

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 (http://en.wikipedia.org/wiki/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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

623 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