• C

purpose/advantage of mmap()

       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?

Who is Participating?
grg99Connect With a Mentor Commented:
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.

brettmjohnsonConnect With a Mentor Commented:
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.

NovaDenizenConnect With a Mentor Commented:
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.

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.

All Courses

From novice to tech pro — start learning today.