What's the practical use of the error close() returns?
Nadav Har'El
nyh at math.technion.ac.il
Fri Jul 27 00:12:14 IDT 2012
On Thu, Jul 26, 2012, Elazar Leibovich wrote about "What's the practical use of the error close() returns?":
> I was always intrigued by this unix tidbit, closing a file can return an
> error. In practice, it is rarely checked (as far as I've seen)
Here are my two cents:
In Unix/Linux, writing to a file usually writes only to kernel memory,
and the actual writing - to disk, to a network file system, or whatever -
only happens later. So an error might be discovered only after a write()
returns.
Now, imagine a program that does a series of writes and finally a
close(). Imagine that sometime in the middle of this, there is a write
error - perhaps the disk is bad, perhaps the filesystem becomes full, or
whatever. If the error comes between two write()s, you'll get it as an
error from the second write(). If it comes between the last write() and
the close(), you'll get it from the close. If it comes *after* the
close(), as far as I know you'll never know that the write failed...
So it seems to me that checking the close() only *sometimes* lets you
know of write errors which you'll otherwise miss. But since you'll
anyway miss other write errors (those coming after the close()), it's
not clear what exactly you're gaining.
BTW, I recently heard a talk in SYSTOR
(http://www.research.ibm.com/haifa/conferences/systor2012/) where the
guy said that for these and other reasons, many programmers started sticking
fsync() calls all over the place, e.g., before close(). But now, these
fsync() are supposed to force not only immediate write from memory to
the hard disk, but also to force actual write to the hard-disk surface
(not just its cache), but this ruins the benefits of tghe hard-disk's
write caching layer, so hard disk manufactures actually started to lie
about writing, when they actually didn't! I.e., you can do an fsync(),
thinking that the write to the hard-disk was successful, when actually
it wasn't done yet, and may fail when actually done.
> Another question is, why let write lie about its success, what does it gain
> you? Let close return void, and force write never to defer its error
> reporting.
If this is what you want, use the O_SYNC or O_DIRECT options to open(2).
Normally, you don't want this because it hurts performance.
--
Nadav Har'El | Thursday, Jul 26 2012, 8 Av 5772
nyh at math.technion.ac.il |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |A language is a dialect with an army.
http://nadav.harel.org.il |
More information about the Linux-il
mailing list