2003-05-28 16:19:46

by John M Collins

[permalink] [raw]
Subject: 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.

--
John Collins Xi Software Ltd http://www.xisl.com



2003-05-28 17:10:53

by Hugh Dickins

[permalink] [raw]
Subject: Re: Question about memory-mapped files

On Wed, 28 May 2003, John M Collins wrote:
>
> 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

2003-05-28 19:06:54

by John M Collins

[permalink] [raw]
Subject: Re: Question about memory-mapped files

Hugh Dickins wrote:

>On Wed, 28 May 2003, John M Collins wrote:
>
>
>>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.
>
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.

--
John Collins Xi Software Ltd http://www.xisl.com



2003-05-29 06:02:21

by Hugh Dickins

[permalink] [raw]
Subject: Re: Question about memory-mapped files

On Wed, 28 May 2003, John M Collins wrote:
>
> 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