posted 14 years ago
To go into a little more detail...
Whenever you make an OS function call (sometimes known as a supervisor interrupt or supervisor call or kernel call), you're doing a major environment switch. Internal hardware states of the CPU are swapped in and out, the actual virtual memory management tables may be altered, various bits of system overhead will be attended to, and control will pass over to the I/O subsystem. Physical I/O is MUCH slower than logical I/O - we're talking thousands, maybe 10s of thousands of times slower. In modern-day OS's using a fixed-block disk architecture, there will usually still be some buffering inside the OS itself, and typically the request will be queued so that the actual I/O can be done using external I/O processors instead of loading down the CPU, but it's still a long, slow process, so doing it it efficiently is important.
Then, on top of that, once the supervisor request is done, the OS kernel will examine the system task queue and determine which task it thinks needs attention. Which probably won't be yours, so there will be further delays before your app comes to the front of the queue again.
So buffering is very important. Just remember to flush the buffers when writing. Because sooner or later, you will need to get the data written to disk or it will all get lost!
I haven't done any measurements on recent-vintage OS's, but on the old IBM mainframes, fully half the time spent executing in some of our apps was spent in the OS itself, not the apps themselves. And that was after we did the stock optimizations.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.