[TUHS] mmap, was SCCS, TeamWare, BitKeeper, and Git

John Levine johnl at taugh.com
Tue Dec 17 05:08:43 AEST 2024


It appears that Douglas McIlroy <douglas.mcilroy at dartmouth.edu> said:
>[Weasel words of my own: I have not read the POSIX definition of mmap.]

We're in luck, I have it right here. On pages 529-530:

2.8.3.2 Memory Mapped Files

Range memory mapping operations are defined in terms of pages. Implementations may
restrict the size and alignment of range mappings to be on page-size boundaries. The page size,
in bytes, is the value of the configurable system variable {PAGESIZE}. If an implementation has
no restrictions on size or alignment, it may specify a 1-byte page size.

Memory mapped files provide a mechanism that allows a process to access files by directly
incorporating file data into its address space. Once a file is mapped into a process address space,
the data can be manipulated as memory. If more than one process maps a file, its contents are
shared among them. If the mappings allow shared write access, then data written into the
memory object through the address space of one process appears in the address spaces of all
processes that similarly map the same portion of the memory object.


The mmap() man page on debian says this, which seems clear enough:

MAP_SHARED
  Share this mapping.  Updates to the mapping are visible to other processes mapping the same region, and (in the case of file-backed mappings) are carried through to
  the underlying file.  (To precisely control when updates are carried through to the underlying file requires the use of msync(2).)


The msync() call is a minor fudge that lets you control how briskly changes are
written back to disk. But it seems clear enough that all the processes that have
a page mapped see the same page, and msync is for databases that want to bs sure
that changes are committed to permananent storage (a term in the POSIX spec)
before unlocking or unmapping.

R's,
John

PS: I can believe there are some versions of linux that screwed up disk cache
coherency, but that just means they don't properly implement the spec, not for
the first time.  I mean, it's not *that* hard to make all the maps point to the
same physical page frame, even on a machine like POWER with reverse page maps.


More information about the TUHS mailing list