OGeek|极客世界-中国程序员成长平台

标题: ios - mmap、msync(MS_ASYNC) 和 munmap [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:16
标题: ios - mmap、msync(MS_ASYNC) 和 munmap

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

但是,如果我立即在该区域调用 munmap,我可以假设 msync 将安全执行吗?或者我必须在 munmap 之前调用 msync 吗?



Best Answer-推荐答案


简短的回答是肯定的——即使你从不调用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 有这样的注释:

Since Linux 2.6.19, MS_ASYNC is in fact a no-op, since the kernel properly tracks dirty pages and flushes them to storage as necessary.

请记住:mmap 将文件系统缓存的页面直接暴露给用户空间,并且与任何其他缓存系统一样,更改将在未来某个时候传播到后备存储。即使您的程序崩溃,您对页面所做的更改最终也会传播。 msync 的使用是为了保护您免受断电等情况的影响。

关于ios - mmap、msync(MS_ASYNC) 和 munmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097316/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4