mmap seems very awesome when you first get to know it. You enter one of those "I just found a new programming technique" phases where you naively want to do all your I/O that way because you have just seen the light.
Then hopefully you start to understand the SIGBUS problem. I/O failure becomes indistinguishable from a bad pointer dereference. Oh wait, maybe I/O and memory really should be separate...
At least that's how I felt about it. From what I see many people do not reach that last phase.
With great power comes great responsibility. mmap is one of those tools.
Keep in mind that your whole linux system essentially mmaps your binaries / shared libraries when you run an application. And with caveats our world still keeps going around.
Error handling with mmap is a PITA but there's a few ways you can work around the general cases:
Use used mapped region for reading data and then use write() for writing it. That's what LMDB does. That's an assumption that's betting errors occurring in the write path.
If you're doing IO in a tight loop you can catch the SIGBUS sent to your thread (SIGBUS/SIGSEGV are always deliver to the thread that caused it). You can deal with the fault via sigsetjmp/siglongjmp. This has all sorts of fun draw backs (like if you're using C++ RAI after sigsetjmp).
> Keep in mind that your whole linux system essentially mmaps your binaries / shared libraries when you run an application. And with caveats our world still keeps going around.
Yes, and it does very admirable things there, brilliant things I would say. If you aren't going to touch the whole thing it doesn't have to load it from disk. If there is memory pressure it can just evict the in-memory copies of pages. All great stuff for that usage.
That said I have seen it cause issues. Most commonly I'd see it on Windows (it's not called mmap there, but whatever, same issue) if you run an app from a network share. Suddenly network timeouts make the whole app blow up. Not cool. There is actually a flag in the EXE file format that says "if you run this from a network, copy the contents to the pagefile first" - meant for exactly this scenario.
Then hopefully you start to understand the SIGBUS problem. I/O failure becomes indistinguishable from a bad pointer dereference. Oh wait, maybe I/O and memory really should be separate...
At least that's how I felt about it. From what I see many people do not reach that last phase.