LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   fflush and fsync? (http://www.linuxquestions.org/questions/programming-9/fflush-and-fsync-378849/)

power123 11-01-2005 03:21 AM

fflush and fsync?
 
Hello....

I want to ask a question on C programming on Linux.

There are 2 functions: fflush and fsync, both said that they are putting the data to devices in the buffers. But what is their main difference??

In fact, I just want to force my data in buffers (after calling fwrite) to be put to the harddisk. What functions should I call??

Thank you so much.

Danny

Hko 11-01-2005 06:51 AM

If you use the high-level (stdio) functions fopen(3), and fwrite(3), then also use the high-level (stdio) fflush(3).

If you are using the low-level open(2) and write(2), then use the low-level fsync(2).

naf 11-01-2005 12:02 PM

Besides the fact that fflush is a high-level function using FILE * and fsync is a low-level function using an int file descriptor, fflush is ANSI C and fsync is a unistd function (POSIX but not ANSI). If you want a platform independent program, avoid fsync, however, if you are using devices, fsync will send the data to the devices whereas fflush relies on C Library (libc).

power123 11-01-2005 08:36 PM

Thank you so much!!

By the way, if I use both of them (call fflush and then call fsync), would it cause any problems??

Thanks again.

Danny.

jim mcnamara 11-02-2005 06:32 PM

No - you have to convert the FILE * to the fd integer using :
int fileno(FILE *fp); <- in stdio.h

You are better off using high level calls with high level File I/O.

ie. -
fopen, fwrite,fclose,fflush all together on the same FILE *.

open,close, write, fsync all togeother on the sanme file descriptor.

Hko 11-02-2005 06:36 PM

Quote:

Originally posted by power123
By the way, if I use both of them (call fflush and then call fsync), would it cause any problems??
No problem, as long as call fflush() only on fopen()-ed FILE* pointers. And fsync() only on file descriptors (int's) opened with open().

gxf 11-03-2005 12:04 AM

that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, e.g. with sync or fsync.

Besides when you use fsync() ,In case the hard disk has write cache enabled, the data may not really be on permanent storage when fsync/fdatasync return.


All times are GMT -5. The time now is 03:21 PM.