Solved

purpose/advantage of mmap()

Posted on 2004-09-30
3
1,238 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 and use conditional statements in the C programming language.

786 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