Search code examples
csystem-callsflushfflush

what is practical differences between flush, write() and fflush()?


In this post, the answer said

Flushing: To sync the temporary state of your application data with the permanent state of the data (in a database, or on disk).

I think that the flush is executed when some buffer is written to an i/o device (like disk) by the write() system call.

So it seems that a data writing to a device with write() and the data flushing to the device are to do the same things.

If so, can I say that the flushing a data with fflush() and the writing the data with write() are completely same?


Solution

  • First, let's do the obvious thing:

    fflush

    For output streams (and for update streams on which the last operation was output), writes any unwritten data from the stream's buffer to the associated output device.

    The C Standard doesn't state how the data is written to the output device. On Posix systems, most likely via write, other systems might have different (similar) interfaces.

    Conceptually speaking, a flush will use the underlying write primitive to transmit the data from the buffer to the output device.

    In short:

    • fflush() the same as write() -> No.
    • fflush() uses write() -> Yes, most likely.
    • fflush() and write() ensures the data to be written to the output device -> Yes.