mmap写入磁盘上的文件(同步/异步)

发布于 2024-08-20 22:05:11 字数 210 浏览 4 评论 0原文

我有关于 mmap 功能的问题。当 mmap 在异步模式下使用时,内核负责将数据保存到磁盘上的映射文件中,是否可以让前面的更新覆盖后面的更新?

假设在时间 T,我们修改内存中的一个位置,该位置是内存映射到磁盘上的文件,并且在时间 T+1 我们再次修改内存中的同一位置。由于对文件的写入不同步,是否有可能内核首先在时间 T+1 获取修改,然后在时间 T 获取修改,从而导致内存映射文件不一致?

I havea question regarding mmap functionality. when mmap is used in asynchronous mode where the kernel takes care of persisting the data to the mapped file on the disk , is it possible to have the former updates overwrite the later updates ?

Lets say at time T, we modify a location in memory that is memory mapped to a file on disk and again at time T+1 we modify the same location in memory. As the writes to the file are not synchronous, is it possible that kernel first picks up the modifications at time T+1 and then picks up the modifications at time T resulting in inconsistency in the memory mapped file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-08-27 22:05:11

这并不完全可能。文件允许不一致,直到msync(2)munmap(2) - 当发生这种情况时, > (修改的)页面逐页写入磁盘(有时更多,取决于较新内核中的文件系统)。 msync() 允许您指定同步操作以及写入完成后使缓存失效,这样可以确保缓存中的数据与文件中的数据相同。如果没有这个,您的程序可能会看到较新的数据,但文件包含较旧的数据 - 这种情况的具体细节取决于 CPU 架构和这些例程的特定操作系统实现。

It's not exactly possible. The file is allowed to be inconsistent till msync(2) or munmap(2) - when that happens, dirty (modified) pages are written to disk page by page (sometimes more, depends on filesystem in newer kernels). msync() allows you to specify synchronous operation and invalidation of caches after finished write, which allows you to ensure that the data in cache is the same as data in file. Without that, it's possible that your program will see newer data but file contains older - exact specifics of the rather hairy situation depend on CPU architecture and specific OS implementation of those routines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文