News for hackers. Stuff that matters.
Re:Why mmap is faster than system calls (2019) by iforgotpassword on Saturday January 09, 2021 @01:25PM (#25702979)
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.

Re:Why mmap is faster than system calls (2019) by jabl on Saturday January 09, 2021 @02:09PM (#25703470)
Indeed. 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.
Re:Why mmap is faster than system calls (2019) by chrchang523 on Saturday January 09, 2021 @03:35PM (#25704448)
Yeah, 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.
Re:Why mmap is faster than system calls (2019) by justin66 on Saturday January 09, 2021 @02:54PM (#25704031)
There’s nothing wrong with using a read only mmap in conjunction with another method for writes.
Re:Why mmap is faster than system calls (2019) by iforgotpassword on Saturday January 09, 2021 @04:13PM (#25704817)
You have exactly the same problem on a read error.
Re:Why mmap is faster than system calls (2019) by justin66 on Saturday January 09, 2021 @05:32PM (#25705665)
Not the problem you described in your second paragraph.
Re:Why mmap is faster than system calls (2019) by iforgotpassword on Sunday January 10, 2021 @04:31AM (#25712796)
Then 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...
Re:Why mmap is faster than system calls (2019) by justin66 on Sunday January 10, 2021 @09:01AM (#25714674)
Reading 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.

Re:Why mmap is faster than system calls (2019) by iforgotpassword on Sunday January 10, 2021 @10:39AM (#25715699)
In 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.
Re:Why mmap is faster than system calls (2019) by klodolph on Saturday January 09, 2021 @05:41PM (#25705751)
You 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.