mmap, msync(MS_ASYNC) and munmap

3

如果我在一个内存映射区域上使用MS_ASYNC调用msync,则同步进程将被异步处理。

然而,如果我立即在该区域上调用munmap,我可以假定msync将安全执行吗?还是我必须在munmap之前调用msync?

1个回答

3
简短的回答是肯定的——即使你从未调用msync,内容的更改最终也会安全地到达文件。摘自 man 2 mmap:
MAP_SHARED
          Share this mapping.  Updates to the mapping are visible to other
          processes that map this file, and are carried through to the
          underlying file.  (To precisely control when updates are carried
          through to the underlying file requires the use of msync(2).)

更重要的是,man 2 msync有以下说明:

自Linux 2.6.19以来,MS_ASYNC实际上是一个空操作,因为内核会正确地跟踪脏页并在必要时将其刷新到存储器中。

请记住:mmap直接将文件系统缓存页面暴露给用户空间,就像任何其他缓存系统一样,更改将在未来某个时刻传播到后备存储器。即使您的程序崩溃,对页面所做的更改最终也会被传播。使用msync可以保护您免受断电等问题的影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接