mmap (source code)
= mmap
{c}
The <POSIX> mapping interface. Ordinary <Linux kernel> <file> mappings require a positive length and an aligned <file offset>. A null address lets the <operating-system kernel> choose an aligned base; length need not be aligned. Check for `MAP_FAILED`. After success the <file descriptor> may be closed without invalidating the mapping; <munmap> or process termination removes it.<mmap(2) — Linux manual page>[DESCRIPTION: address, length, offset and descriptor lifetime; munmap; RETURN VALUE]{ref}
Mapping does not extend the <backing file>. Under <POSIX>, for an initially partial last <memory page>, the tail beyond the <file>'s end is zero-filled and is not written back as <file> data; accesses to whole mapped <memory pages> beyond the end raise `SIGBUS`. <POSIX> leaves access to portions added or removed by later resizing unspecified. Establish the required <file> size first and coordinate resizing with readers; a mapping's length is not proof that those <bytes> exist in the <file>.<The Open Group Base Specifications Issue 8, IEEE Std 1003.1-2024 by IEEE and others/mmap — POSIX.1-2024>[POSIX.1-2024, DESCRIPTION: partial end page, whole pages beyond the object, and changed file size; RATIONALE: references beyond the end do not extend the object]{ref}
The <Linux kernel> documents a tail-cache exception: modified beyond-end <bytes> can survive in the <page cache> and appear in later mappings, although never written to the <file>. Do not use that tail as reliable storage.<mmap(2) — Linux manual page>[BUGS: modified contents beyond the object end can remain in the page cache]{ref}
Back to article page