Solved

purpose/advantage of mmap()

Posted on 2004-09-30
3
1,194 Views
Last Modified: 2010-08-05
Hi,
       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?

Thanks.
0
Comment
Question by:dkamdar
3 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
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.

0
 
LVL 22

Assisted Solution

by:NovaDenizen
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.

0
 
LVL 22

Accepted Solution

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

 
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and reading files in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now