Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to keep a file open after calling mmap on it?

Tags:

c

mmap

I have a program that maps quite a few (100's) of sizable files 10-100MB each. I need them all mapped at the same time.

At the moment I am calling open followed by mmap at the beginning of the program, followed by munmap and close at the end.

Often I have to adjust the open files limit running ulimit -n before running the program.

Question is do I actually need to keep the files open, or can I open mmap close do some large data processing then munmap when I'm finished.

The man pages of mmap do not seem terribly clear to me on this one.

like image 334
camelccc Avatar asked Jul 05 '13 13:07

camelccc


People also ask

Can you close a file after mmap?

After the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.

How does mmap file work?

mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.

Does mmap load file into memory?

Yes, mmap creates a mapping. It does not normally read the entire content of whatever you have mapped into memory. If you wish to do that you can use the mlock/mlockall system call to force the kernel to read into RAM the content of the mapping, if applicable.


1 Answers

No, at least not on Linux it's fine to close the file.

The manual page clearly states:

On the other hand, closing the file descriptor does not unmap the region.

For portability, I also checked the POSIX manual, it says the same thing (although even more clearly):

The mmap() function adds an extra reference to the file associated with the file descriptor fildes which is not removed by a subsequent close() on that file descriptor. This reference is removed when there are no more mappings to the file.

like image 184
unwind Avatar answered Sep 29 '22 01:09

unwind