Question about memory-mapped files
Please cc me ([email protected]) in any reply as I'm not subscribed.
Could someone advise me on the answer to the following question:
If I invoke mmap to map a file to memory, and it succeeds, can I safely close the original file descriptor and rely on the memory still being mapped and the file still updated (possibly with mysnc)?
I've looked through the kernel (2.4) source and it seems I can. I've tried a test program on my machine and also Solaris and HP and it works OK the file getting updated.
On the Linux machine /proc/<pid>/maps seems to have all the right stuff in after the file is closed.
The only thing that doesn't happen is that the file mod time doesn't get changed (on any machine).
Of course "munmap" and "mremap" don't oblige you to pass an fd so it would seem logical. But no manual page actually seems to say it.
Could anyone advise? Thanks.
Signature · 1 line
John Collins Xi Software Ltd www.xisl.com
Quoted 4 lines · John M Collins
If I invoke mmap to map a file to memory, and it succeeds, can I safely close the original file descriptor and rely on the memory still being mapped and the file still updated (possibly with mysnc)?
Yes, that's definitely a part of the specification of mmap, even if it's not mentioned on the man page.
Note that the file on disk is likely not to be updated until some time after you unmap it, unless you use msync to force it.
Hugh
Quoted 7 lines · Hugh Dickins
Quoted 3 lines · John M Collins
If I invoke mmap to map a file to memory, and it succeeds, can I safely close the original file descriptor and rely on the memory still being mapped and the file still updated (possibly with mysnc)?
Quoted 6 lines
Yes, that's definitely a part of the specification of mmap, even if it's not mentioned on the man page.
Note that the file on disk is likely not to be updated until some time after you unmap it, unless you use msync to force it.
Thanks - FYI the file mod time eventually got updated on HP-UX but not on Solaris (2.9) or Linux (2.4.21) - and it doesn't seem to update it even if you don't close the f.d. I think that has to be wrong if the manual page is anything to go by.
Signature · 1 line
John Collins Xi Software Ltd www.xisl.com
Quoted 5 lines · John M Collins
Thanks - FYI the file mod time eventually got updated on HP-UX but not on Solaris (2.9) or Linux (2.4.21) - and it doesn't seem to update it even if you don't close the f.d. I think that has to be wrong if the manual page is anything to go by.
I agree with you. I hate files being modified without mtime changing, and the mmap(2) man page is so surprisingly specific about those times that I bet the text comes directly from one of the standards.
It's not something I'm rushing to fix; but I've made a note to look at it later (days not hours) if nobody else gets there first. It may turn out to be too difficult to get right - mmap and [amc]time don't fit well together; but that text you've noticed at least gives something to aim for - thanks.
Hugh