简体   繁体   中英

How is a memory mapped file synchronized with another process writing to file?

I am essentially trying to "tail" a file by using a memory mapped file within a reader process while a writer process is appending data to it (using stdio) at some time interval.

My question is how does the synchronization between the memory mapped file and the writer work behind the scenes? Would I need any additional synchronization mechanisms?

I could use inotify to find out when the file is modified but I wasn't sure if the memory mapped file is guaranteed to be updated as well.

I am using Linux ubuntu with gcc 4.8.2.

If you are writing into a memory-mapped file, the file be backed up to disk in the following cases (I assume you mapped the file with MAP_SHARED ):

  1. Using msync (see http://man7.org/linux/man-pages/man2/msync.2.html )
  2. When the memory page is swapped-out: the system need memory and if the memory page is a file, it will be saved to the original location.
  3. When you munmap (see http://man7.org/linux/man-pages/man2/munmap.2.html

In either case, you should use msync to flush the data to disk and notify any programs watching on the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM