How to flush to disk without sync?

flush without sync

  • From what I've read, flush pushes data into the OS buffers and sync makes sure that data goes down to the storage media. So, if you want to be sure that data is actually written to disk, you need to do a flush followed by a sync. So, are there any cases where you want to call flush but not sync?

  • Answer:

    You only want to http://linux.die.net/man/3/fflush if you're using stdio's FILE *. This writes a user space buffer to the kernel. The other answers seem to be missing http://linux.die.net/man/2/fdatasync. This is the system call you want to flush a specific file descriptor to disk.

Eduardo at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Yes, lots. Most programs most of the time would not bother to call any of the various sync operations; flushing the data into the kernel buffer pool as you close the file is sufficient. This is doubly true if you're using a journalled file system. Note that flushing is a higher level operation than the read() or similar system calls. It is used by the C <stdio.h> library, or the C++ <iostream> library. The system calls inherently flush the data to the kernel buffer pool (or direct to disk if you're using direct I/O or something similar). Note, too, that on POSIX-like systems, you can arrange for data sync etc by setting flags on the http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html system call (O_SYNC, O_DSYNC, O_RSYNC), or subsequently via fcntl().

Jonathan Leffler

When you fflush, you flush the buffer of one file to disk (unless you give NULL, in which case it flushes all open files). http://www.manpagez.com/man/3/fflush/ When you sync, you flush all the buffers to disk. http://www.manpagez.com/man/2/sync/ The most important thing that you should notice is that fflush is a standard function, while sync is a system call provided by the operating system (Linux for example). So basically, if you are writing portable program, you in fact never use sync.

Shahbaz

Just to clarify, fflush() applies only when using the FILE interface of UNIX that buffers writes at the application level. In case the normal write() call is used, fflush() makes little sense. Having said that, I can think of two situations where you would like to call fflush() but not sync: You want to make sure that the data will eventually make it to disk even though the application crashes. Force to screen the data that the application has written to standard output so far. The second case is the most common use I have seen and it is usually required if the printf() call does not end with a new line character ('\n').

luis

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.