The standard C function fflush() and the POSIX fsync() system call are conceptually somewhat similar. fflush() works with C file streams ( FILE objects) and is therefore portable. fsync() works with POSIX file descriptors. Both cause the sending of buffered data to the destination.
On a POSIX system, each C file stream has a associated file descriptor , and all operations on the C file stream will be implemented by delegating, if necessary, POSIX system calls that operate on the file descriptor.
You might think that calling fflush on a POSIX system will write any data to the file stream buffer, then calling fsync() on the file descriptor for that file stream. Thus, on a POSIX system, it would not be necessary to follow the fflush call using the fsync(fileno(fp)) call. But is this so: is there a fsync call from fflush ?
No, calling fflush on a POSIX system does not mean that fsync will be called.
C standard for fflush says (highlighted)
causes any unwritten data for stream [] to be transferred to the host environment for writing to a file
The statement that data should be written, not written, implies that further buffering by the host environment is allowed. This buffering by the host environment may include internal buffering for the POSIX environment that fsync flushes. So, a careful reading of the C standard suggests that the standard does not require the implementation of POSIX fsync .
The standard description of fflush POSIX does not declare, as an extension of the semantics of C , that fsync is invoked.
Raedwald Jan 12 '17 at 2:03 on 2017-01-12 14:03
source share