mmap(2) questions, reads not caching
Dan Nelson
dnelson at allantgroup.com
Mon Sep 8 20:36:47 PDT 2003
In the last episode (Sep 08), Sean Hamilton said:
> I have some code resembling:
>
> FILE * f = fopen (filename, "rb");
> mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fileno (f), 0);
>
> I've found that reads are not brought into disk cache. Successive
> reads on the same file once again read from disk. If I cat the file
> to /dev/null, then the mmap(2) does indeed read the data from cache.
> What's going on here?
mmap just maps a file to memory. It doesn't force the OS to cache the
pages any more than it already does. You can try calling
madvsize(M_WILLNEED) and see if that helps.
> Also, the man page states that the mapped region may be longer than the
> specified size. Does this have any implications for the size which is passed
> to munmap(2)? If I pass the same size to munmap(2), then will there still be
> leftover, or will the entire region be unmapped?
It simply rounds up to the nearest pagesize. If you map 1 byte, you
get a page. munmap does the same thing.
> And, should I be passing MAP_PRIVATE or MAP_SHARED to read-only mmaps? Does
> it make any difference at all?
I don't think it matters.
--
Dan Nelson
dnelson at allantgroup.com
More information about the freebsd-hackers
mailing list