Alain Bonnefoy <email@example.com> wrote:
so, I don’t why exactly, but doing fwrite() is not the same as doing write().
fwrite() will copy the bytes passed to it into the buffer scheme that
FILE * implies, then IF the buffer is full (or has hit a flush point),
will call write() to send the buffer to the file manager process.
write() will immediately send the data to the file manager process.
fwrite() is client-side buffered I/O. write() is un-buffered on the
client-side. In both cases, the file manager process may buffer (cache)
the data before it hits the hardware. (Some control availabe with things
like O_DYSNC open() flag.)
In first case, output is not necessarily done in case of
abnormal termination (after return of fwrite() of course!).
FILE * buffers are NOT flushed on abnormal termination. They are on
In the second case, there is no problem.
Maybe it’s not described in the C standard, but it works like that on
QNX6. I have not enough experience to talk about how that works on
The above is normal UNIX behaviour. I had encountered it well before
joining QNX… standard gotcha of printf() debugging, want to find where
program is crashing, but getting lots of printfs, so redirect stdout to
a file, and all of a sudden you don’t get the last few printfs before
the program crashes. That was because another normal UNIX behaviour
is that output is line buffered to a terminal (flush on new line) but
“fully” buffered to a file (flush on end of 1024 byte buffer) and doesn’t
flush on abnormal termination.
If you don’t want client-side buffering, don’t use the FILE * calls. They
are less efficient, usually resulting in an extra copy of the data. There
are places where they can be helpful or useful. For instance, you will
get more data onto a flash filesystem if you use larger writes rather than
smaller ones – so if you are generating output for a file on a flash file
system, having your output buffered and delivered in 1k chunks, rather than
as several smaller chunks will make for more efficient storage, without you
having to write the code to do the client-side buffering.
QNX Training Services
I do not answer technical questions by email.