如果进程崩溃,mmaped文件会发生什么?

时间:2022-11-22 20:58:01

This may have a different answer between OSX, Windows and Linux.

这可能在OSX、Windows和Linux之间有不同的答案。

If the process crashes, will dirtied pages from the mmap be discarded or written out eventually by the OS, assuming it does not crash?

如果进程崩溃了,那么,如果不崩溃,那么从mmap中删除的页面会被丢弃,或者最终被操作系统注销吗?

It's clear that they persist if another process has mapped them, but what if the crashed process was the only one? I'm interested both in what is technically promised in docs and what the implementation actually does. If you only know for one OS please respond for just that one.

很明显,如果另一个进程映射了它们,那么它们仍然存在,但是如果崩溃的进程是唯一的进程呢?我对文档的技术承诺和实现的实际功能都很感兴趣。如果你只知道一个操作系统,请只回复那个。

1 个解决方案

#1


1  

For Windows, I don't think there is any doubt that dirty pages are eventually written to disk. It is explictly documented that unmapping a file view does not flush the data, but the data will be written lazily to disk as with any cache. FlushViewOfFile forces an immediate write, but calling it is optional.

对于Windows,我不认为有任何疑问,脏页最终被写入磁盘。有明确的文档说明,取消映射文件视图不会刷新数据,但数据将像使用任何缓存一样被延迟写入磁盘。FlushViewOfFile强制立即写入,但调用它是可选的。

There are exceptions which may or many not be relevant. Two mapped views of a file are guaranteed to remain coherent even if one program terminates abnormally, but this coherency does not extend to remote files or to files accessed concurrently using ReadFile/WriteFile.

有些例外可能是相关的,也可能是不相关的。即使一个程序异常终止,文件的两个映射视图也保证保持一致,但是这种一致性不会扩展到远程文件或使用ReadFile/WriteFile并发访问的文件。

The documentation does not provide an explicit answer, but neither does it give any hint that the opposite might be true. I would rely on, subject to testing.

文档没有提供明确的答案,但也没有给出任何相反的可能是真的的暗示。我会依靠,但要经过测试。


And as pointed out in a link, if there is a risk of machine failure it might be a good idea to make sure the pages get flushed as they are written. Kernel flushing the cache could be delayed quite a while.

正如在一个链接中指出的,如果存在机器故障的风险,最好确保页面在编写时被刷新。内核刷新缓存可能会被延迟一段时间。

#1


1  

For Windows, I don't think there is any doubt that dirty pages are eventually written to disk. It is explictly documented that unmapping a file view does not flush the data, but the data will be written lazily to disk as with any cache. FlushViewOfFile forces an immediate write, but calling it is optional.

对于Windows,我不认为有任何疑问,脏页最终被写入磁盘。有明确的文档说明,取消映射文件视图不会刷新数据,但数据将像使用任何缓存一样被延迟写入磁盘。FlushViewOfFile强制立即写入,但调用它是可选的。

There are exceptions which may or many not be relevant. Two mapped views of a file are guaranteed to remain coherent even if one program terminates abnormally, but this coherency does not extend to remote files or to files accessed concurrently using ReadFile/WriteFile.

有些例外可能是相关的,也可能是不相关的。即使一个程序异常终止,文件的两个映射视图也保证保持一致,但是这种一致性不会扩展到远程文件或使用ReadFile/WriteFile并发访问的文件。

The documentation does not provide an explicit answer, but neither does it give any hint that the opposite might be true. I would rely on, subject to testing.

文档没有提供明确的答案,但也没有给出任何相反的可能是真的的暗示。我会依靠,但要经过测试。


And as pointed out in a link, if there is a risk of machine failure it might be a good idea to make sure the pages get flushed as they are written. Kernel flushing the cache could be delayed quite a while.

正如在一个链接中指出的,如果存在机器故障的风险,最好确保页面在编写时被刷新。内核刷新缓存可能会被延迟一段时间。