0

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?

3088 K
  • 75
  • 5
  • 1
    Think of `flush` as meaning **commit**; you can write as much as you want, but unless you commit those changes then they'll remain in-memory and won't be reflected by the actual data. – radj307 Dec 07 '22 at 03:12

1 Answers1

1

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.
Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11