For example, I use mmap to map a file into the memory as read-only shared mode. And I read some data on the file based on their address in the memory. What if I call write and fsync to update or changes the underlying file after then? Will the data in the memory also change? Or I need to call some other system call to synchronize the mapped memory? I find this question when reading source code of BoltDB.
-
Have a look at https://stackoverflow.com/questions/22678981/concurrently-writing-to-file-while-reading-it-out-using-mmap – Jun 24 '19 at 02:06
-
@DavidCullen Thank you! It seems like I'd better stick to one kind of system call instead of mixing them. – chenyuandong Jun 24 '19 at 02:45
1 Answers
The same question bothered me when I was reading the source code of boltdb. I searched/reviewed a lot of articles on Google but still didn't get an answer.
I knew that linux has a dnotify
/inodtify system to monitor file changes, thus it is sure that kernel knows changes made to a file.
The man page for mmap didn't describe the satiation mentioned here, but the description for MAP_SHARED did mention that
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.
I think it should be a reasonable guess that MAP_SHARED guarantees changes made to the underlying file are seen to all mapped memory regions. Hope someday a Linux kernel expert can provide some details on this.

- 721
- 8
- 13