mmap

hai,

  How do we map 'n' number of files into memory by using mmap system call??

Thanks in advance......

You don't.

The mmap() system call maps a file based on an open file descriptor - which only refers to one file.

Is it possible to map two open file fd by using two mmap system calls?

Sure you can. Run an strace or truss on any process when it starts up and see all the shared objects get mmap()'d into your address space.

could you explain the concept more?(Like this is it possible to map more number of files into memory)

I'm reminded of the old story... A man decides to learn how to juggle, so gets a book on it. Page one, it says "pick up one ball, throw it from the left hand to the right hand and back, and don't let it hit the ground". So he does and turns the page. It tells him to pick up two balls, and he does, and it works. The next pages tell him to pick up three, four, and finally, five -- but someone had torn out the rest of the pages, so he never learns how to juggle twelve.

To map two files, you call mmap twice, on two different file descriptors. To map five files, you call mmap five times, on five different file descriptors. And so forth.

int fd1=open("filename1", O_RDONLY);
void *mem1=mmap(fd1, ...);
int fd2=open("filename2", O_RDONLY);
void *mem2=mmap(fd2, ...);
int fd3=open("filename3", O_RDONLY);
void *mem3=mmap(fd3, ...);
...

And if you really have a lot of them, you could read filenames from a file and store the file descriptors and mmap-ed memory pointers in an array or vector.

You could also structure your data, so you can keep more than one thing in a single file instead of mapping lots of seperate ones.

Also keep in mind there are limits to the amount of memory you can map on a 32-bit system. Keep one process below a few hundred megs and you should be relatively safe. Now 64-bit systems, they can safely map truly absurd amounts of memory -- xfs.fsck can map in entire multi-terabyte disk arrays to do its dirty work, for example, but crashes if you try it on a 32-bit system.

1 Like