Eloff
Eloff

Reputation: 21656

What happens to mmaped file if process crashes?

This may have a different answer between OSX, Windows and 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?

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.

Upvotes: 20

Views: 3258

Answers (1)

david.pfx
david.pfx

Reputation: 10868

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.

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.

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.

Upvotes: 2

Related Questions