2

I'm getting scenario in macOS where I cannot close memory mapped file without un-mapping it first (using munmap syscall), although the close syscall return success result (=0) I can still see the file record in lsof -n.

In linux, it's explicitly mentioned that closing the file doesn't unmap the file, according to mmap man page.

Is it indeed a different behavior between the 2 OSes ? Is there any explanation for this different behavior ?

EDIT: after reading the comments below, I've realized that there's no different behavior between the platforms, and that the reason why my file still opened is because it's still referenced by the mmap.

thanks

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • Could it be that the usage pattern is different in your app between Linux vs. MacOS? I would expect that closing *and* unmapping the file would be necessary before the file is actually closed. Linux is probably indifferent to the order, but maybe MacOS cares which is done first. – wallyk Oct 19 '17 at 21:10
  • 1
    The file is still in use, because of the mmap... All you did do is close adescriptor. – Antti Haapala -- Слава Україні Oct 19 '17 at 21:10
  • Did you test it on Linux? I've got *2* entries in `lsof -n` output – Antti Haapala -- Слава Україні Oct 19 '17 at 21:20
  • @anttihaapala, no i just assume that it worked according to their man page, but according to the answer below, this behavior seems reasonable. – Zohar81 Oct 19 '17 at 21:22
  • How is the behavior you describe a difference between Linux and Mac? In both cases, closing the file does not unmap it. In both cases, an open file description remains in the system after the (successful) `close()` call. Or at least that's what I expect, and it seems to be consistent with what you said. – John Bollinger Oct 19 '17 at 21:28
  • @johnbollinger, i assumed there's a difference according to the documentation, but now I've realized that this is in fact the same behavior and the file remains open for my process simply because it's still has one reference. – Zohar81 Oct 19 '17 at 21:32
  • Besides:mmap() stems from BSD, and OSX started as a BSD rip-off. https://en.wikipedia.org/wiki/Mmap https://stackoverflow.com/a/8507066/905902 – wildplasser Oct 19 '17 at 22:10

1 Answers1

3

POSIX requires that there's a reference to mmap'ed file even after a close.

The mmap() function shall add 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 shall be removed when there are no more mappings to the file.

And that's what lsof sees there's a reference to that file. So it's working as documented.

endolith
  • 25,479
  • 34
  • 128
  • 192
P.P
  • 117,907
  • 20
  • 175
  • 238
  • my process actually open all the files in the mount-point, mapping them and then closing them. Therefore, when closing the file without unmapping it first, i get the "Too many opened files in the system" message. so now i understand that this is expected .. but what if i try to unmap according to file descriptor after close, should it work ? – Zohar81 Oct 19 '17 at 21:21
  • 1
    @Zohar81 :open() increments the reference count for the file. mmap() too (now there are two refences); close() decrements it (now:1). after munmap() the count will drop to zero. – wildplasser Oct 19 '17 at 21:27
  • You can try to [increase the limit](https://superuser.com/a/443168) on your system. You obviously don't want to have too many mapped files or open file descriptors. Closing the file isn't directly linked to unmapping the same file. You can close immediately after mmap'ing it. Then munmap it when you need to. – P.P Oct 19 '17 at 21:29
  • @Zohar81, how would you use the file descriptor to unmap the file in any case? `munmap()` requires you to specify *the address of the mapping* to identify what to unmap. But if you do unmap the file that way, then yes, that should free up an open file description slot. – John Bollinger Oct 19 '17 at 21:31
  • @johnbollinger, you're right, i need to use the address from returned when i first mapped the file ... fd wouldn't help here. – Zohar81 Oct 19 '17 at 21:33