Quote:
Originally Posted by
Humudituu
I'm trying to wrap my mind around this... The mutex should be released after the lseek, right? Is the mutex active while writing? Otherwise the behaviour explanied below wouldn't make sense to me, as either lseek while reading would be slow as well or the mutex should be released rather quickly... :S
I suspect reads are happening faster than writes because a disk has its own internal cache, too.
That mutex must control more than just the a offset...
On thinking about this a little more, I think this happens because POSIX requires the ordering of some block operations to be preserved. It's pretty much just common-sense rules, like if one program reads a block after another writes to it, the reading program should get the new contents and not the old.
Forcing things to go in order is easy
when you have cache. Just keep the cache consistent and everything's golden. Things don't have to wait for each other. Reads still happen randomly as needed, while disk writes happen in orderly groups, at times of the kernel's own choosing. ext2/3/4 are designed for this mode of operation.
When you switch to direct I/O, writes
must happen in lock-step for consistency to be preserved. The order of reads doesn't matter as much.
I suspect you'd get better performance by writing to a raw disk device instead of a file on disk. That's the context I usually see O_DIRECT employed in.