4 ms·
Also error handling. read and write can return errors, but what happens when you write to a mmaped pointer and the underlying file system has some issue? Assign
by iforgotpassword 6y ago
Also error handling. read and write can return errors, but what happens when you write to a mmaped pointer and the underlying file system has some issue? Assigning a value to a variable cannot return an error.
So you get a fine SIGBUS to your application and it crashes. Just the other day I used imagemagick and it always crashed with a SIGBUS and just when I started googling the issue I remembered mmap, noticed that the partition ran out of space, freed up some more and the issue was gone.
So you might want to set up a handler for that signal, but now the control flow suddenly jumps to another function if an error occurs, and you have to somehow figure out where in your program the error occurred and then what? Then you remember that longjmp exists and you end up with a steaming pile of garbage code.
Only use mmap if you absolutely must. Don't just "mmap all teh files" as it's the new cool thing you learned about.
- jabl 6y agoIndeed. The issue with file resizing I mentioned was mostly related to error handling (what if another process/thread/file descriptor/ truncates the file, etc.). But yes, there are of course other errors as well, like the fs running out of space you mention.
- justin66 6y agoThere’s nothing wrong with using a read only mmap in conjunction with another method for writes.
- iforgotpassword 6y agoYou have exactly the same problem on a read error.
- justin66 6y agoNot the problem you described in your second paragraph.
- iforgotpassword 6y agoThen what do you think happens when you read from your mapped memory and the file system is corrupted and returns an error, or the drive has a bad sector, or the nfs server acts up...
- justin66 6y agoReading from a busted file system is a problem to be dealt with (or not), yes, and I certainly wouldn’t recommend mmaping a file shared over nfs if you can help it. I’m not sure what the use case is where that would seem like a good idea. C, pointers, and mmap are dangerous, sharp instruments but I have to wonder who some of these dramatic warnings are for.
- iforgotpassword 6y agoIn general we can probably agree you should always check the return value of read() and write() and handle errors. At least just perror() and abort(), so the user has a chance at finding the problem. Similarly, using mmaped files without handling errors is user hostile since it just crashes the app and SIGBUS gives absolutely no clue to the user what happened. As said, my point is to use mmap when it really makes sense and is worth the hassle, not just because it seems cool and makes the code look a little simpler, exactly because you omit error handling. Especially if you don't know how people will use your software. As you said, mmap on nfs is bad, so you'd basically have to forbid users from using your software with network shares.
- chrchang523 6y agoYeah, this is the biggest reason I stay the hell away from mmap now. Signal handlers are a much worse minefield than error handling in any standard file I/O API I've seen.
- klodolph 6y agoYou don’t have to longjmp, you can remap the memory and set a flag, return from the signal handler, handle the error later, if you like.