菜鸟教程小白 发表于 2022-12-13 02:16:24

ios - mmap、msync(MS_ASYNC) 和 munmap


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

<p>但是,如果我立即在该区域调用 munmap,我可以假设 msync 将安全执行吗?或者我必须在 munmap 之前调用 msync 吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>简短的回答是肯定的——即使你<em>从不</em>调用<code>msync</code>,对内容的更改最终会(并且安全地)进入文件。来自<code>man 2 mmap</code>:</p>

<blockquote>
<pre><code>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).)
</code></pre>
</blockquote>

<p>也许更重要的是,<code>man 2 msync</code> 有这样的注释:</p>

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

<p>请记住:<code>mmap</code> 将文件系统缓存的页面直接暴露给用户空间,并且与任何其他缓存系统一样,更改将在未来某个时候传播到后备存储。即使您的程序崩溃,您对页面所做的更改最终也会传播。 <code>msync</code> 的使用是为了保护您免受断电等情况的影响。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - mmap、msync(MS_ASYNC) 和 munmap,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26097316/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26097316/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - mmap、msync(MS_ASYNC) 和 munmap