Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > The Linux Kernel > MMAP usage

Forgot Password?
 The Linux Kernel   Compiling, theory, programming or other discussion about the linux kernel

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 08-08-2008   #1 (permalink)
Just Joined!
 
Join Date: Sep 2007
Posts: 2
Post MMAP usage

What is an MMAP system Call? Does this avoid any overhead to the system ?
Will this be possible in no-MMU system ?

please provide your inputs,i am confused.
kanagarajan is offline  



Reply With Quote
Old 08-20-2008   #2 (permalink)
Just Joined!
 
Join Date: Jun 2008
Location: North East U.S.
Posts: 25
mmap

The mmap interface is normally used to map a file into memory so that then you can read and/or write the data in the file by accessing memory - no need for file I/O directly. This uses paging interrupts. When you access a page for the first time, it cases a page fault and it reads in that page from your file. If you intend to access the whole file, it may make more sense to read it into memory rather than use mmap. You gain with mmap if you may make sparse accesses into the data. Another advantage of mmap is that multiple processes can share the same data when they all map the same file into their address space. This is similar to shared memory in that respect.

One other use of mmap is to obtain memory (kind of a substitute for malloc) in which you open and map the special file /dev/zeros.

I hope this helps.
bkeller is offline   Reply With Quote
Old 09-15-2008   #3 (permalink)
Just Joined!
 
Join Date: Sep 2008
Posts: 8
Quote:
Originally Posted by kanagarajan View Post
What is an MMAP system Call? Does this avoid any overhead to the system ?
Will this be possible in no-MMU system ?

please provide your inputs,i am confused.
Im not clear about the first parameter (void * address) of mmap.What does it means that address gives a preferred starting address for the mapping.Does it means that mmap map thr region to the location pointed by address?And what is use of flag MAP_FIXED?
chetan.c.thorat is offline   Reply With Quote
Old 09-15-2008   #4 (permalink)
Just Joined!
 
Join Date: Sep 2008
Posts: 8
Suppose I have a file just.txt with containts ABCDEFGH and I mmaped it.mmap gives me a pointer that point to starting of this file just.txt i.e. to A.Should I use that pointer as pointer to array of character or not ? If yes then what happen if Im modifying the file by incrementing that pointer by 2 and it pointing to C,I want to store the changes to file.I have used munmap but it not changes the file,because next time when I mmap the same file it reads from A nto from C.
Another is what happen if I provide a void pointer as first argument to mmap instead of NULL?
chetan.c.thorat is offline   Reply With Quote
Old 09-16-2008   #5 (permalink)
Just Joined!
 
Join Date: Jun 2008
Location: North East U.S.
Posts: 25
Questions on use of mmap()

The first parameter to mmap is a pointer you provide. If it is zero/NULL, mmap gets its own area of memory to use and you gain access to this memory sort of like using malloc() except it comes already filled in with the data from your file (or all zeros if you map the file /dev/zero). If you get your own storage (say via malloc() ), you can use that and mmap will try to map your file there - just be aware that memory maps must start on full page boundaries and will extend to a full page boundary. I think on Linux pages are always 4096 (4K) bytes, but on Solaris you can find 8192 (8K) page sizes. If mmap cannot fit your mapped data into the memory area your pointer points to, it disregards it and finds its own free area within the process address space that is large enough to hold all the data being mapped.

If mmap() obtains the chunk of memory for you, it is freed when you munmap it and you will get a memory fault if you try to access it after it is unmapped. If you provided the memory for the mapping, it is still available for you to use after you munmap it.

I hope this helps.
bkeller is offline   Reply With Quote
Old 09-17-2008   #6 (permalink)
Just Joined!
 
Join Date: Jun 2008
Location: North East U.S.
Posts: 25
My memory is failing me. Don't pass in an address that you obtained via malloc (unless you have also freed it). I believe the address you pass into the mmap() function should be for an address that is not already in use within your processes' address space. Note that this is a recommended address for mmap to use unless you also specify MAP_FIXED in the flag parameter. In my experience, I have only specified a non-NULL pointer for the addr (first) parameter when I have previously gotten that address from mmap() and have subsequentily unmapped that region. If you have some other means of knowing your specified address and full range is not in use, then you may be able to get mmap to use that address. If you specify an address to use and also MAP_FIXED, it may fail if there is not sufficient space before running into some block of memory already in use within your process address space. For 32-bit processes you need to be careful when trying to map large files - there may not be a sufficiently large contiguous unused block of address space in which to map the file.

Brion
bkeller is offline   Reply With Quote
Old 09-22-2008   #7 (permalink)
Just Joined!
 
Join Date: Sep 2008
Posts: 8
Thanks bkeller for your va;uable help related to mmu.It really works.
chetan.c.thorat is offline   Reply With Quote
Old 09-22-2008   #8 (permalink)
Just Joined!
 
Join Date: Sep 2008
Posts: 8
Quote:
Originally Posted by chetan.c.thorat View Post
Thanks bkeller for your va;uable help related to mmu.It really works.
What is the use of the function sysconf();.What is the meaning of parameter _SC_PAGE_SIZE in it
chetan.c.thorat is offline   Reply With Quote
Old 09-23-2008   #9 (permalink)
Just Joined!
 
Join Date: Jun 2008
Location: North East U.S.
Posts: 25
The sysconf() function is used to see values for certain system configuration variables. For most systems, including linux, you can get the page size by calling getpagesize(). I used sysconf() to get page size only for HPUX because that system did not support the getpagesize() function (this was early 1990's). Note that memory mapping is always in integer multiples of page size.
bkeller is offline   Reply With Quote
Old 09-24-2008   #10 (permalink)
Just Joined!
 
Join Date: Sep 2008
Posts: 8
Quote:
Originally Posted by bkeller View Post
The sysconf() function is used to see values for certain system configuration variables. For most systems, including linux, you can get the page size by calling getpagesize(). I used sysconf() to get page size only for HPUX because that system did not support the getpagesize() function (this was early 1990's). Note that memory mapping is always in integer multiples of page size.
Im using msync() to flushes changes made in the file to the disk,but Im getting something wrong.First parameter of msync is the starting address from where we want to update ,second is length of that segment, third is flag.Now suppose I put a string say "abc" to this atarting address,only 'a' is going to update or written to disk.Why is so?
chetan.c.thorat is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
A Newbie's Getting Started Guide to Linux
Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it.
subscribe
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:00 AM.






© 2000 - - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.1