chenyuandong
chenyuandong

Reputation: 296

Is it safe to use mmap with fsync? (will fsync invalidate some page on mmap?)

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.

Upvotes: 2

Views: 1180

Answers (1)

VicX
VicX

Reputation: 711

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.

Upvotes: 0

Related Questions