Reputation: 7192
Under what circumstances is the physical memory backing mmap of the same file shared between processes in linux?
I understand, that the necessary condition is MAP_SHARED
.
I understand, that if the processes use same file descriptor (e.g., child inheriting it from parent), the memory will be shared.
But what happens if, e.g.,
mmap
s the file with PROT_READ
and other with PROT_WRITE
?Upvotes: 1
Views: 1527
Reputation: 2477
See this part of the Linux man page for mmap()
:
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 answer your specific questions:
mmap(MAP_SHARED)
.O_RDWR
mode of the file descriptor controls whether it's permitted for that process to pass PROT_WRITE
, otherwise the mmap()
call from that process will fail with EACCESS
, (from the same man page):Errors:
EACCES
file mapping was requested, but fd is not open for reading. MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode. Or PROT_WRITE is set, but the file is append-only.
PROT_READ
/PROT_WRITE
flags control the readability/writeability of the virtual page table entries created in the corresponding process who made the mmap()
call (and hence that process's ability to read and write). This has no effect on other processes mapping the same file pages (who might differ in read/write access based on the flags to their own mmap
call).Authoritative pages with additional information and some useful examples:
Upvotes: 2