Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 298
Eloff Avatar asked Dec 01 '25 02:12

Eloff


1 Answers

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.

like image 193
david.pfx Avatar answered Dec 02 '25 15:12

david.pfx