79

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
camelccc
  • 2,847
  • 8
  • 26
  • 52
  • You can see it in a system call trace (e.g. `strace`) of an executable, that the mappings for shared libraries follow the pattern: open the descriptor, mmap it, close the descriptor. – Kaz Aug 23 '21 at 20:20

1 Answers1

85

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.

wim
  • 338,267
  • 99
  • 616
  • 750
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 5
    why `proc//fd` does not contain information on the mmapped files? Is it because `mmap` is a `syscall`, so these fds are on kernel's data structures? _asking for a friend_ – Paschalis Jan 20 '17 at 01:24