LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2005, 03:21 AM   #1
power123
LQ Newbie
 
Registered: Nov 2005
Posts: 2

Rep: Reputation: 0
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
 
Old 11-01-2005, 06:51 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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).
 
Old 11-01-2005, 12:02 PM   #3
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
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).
 
Old 11-01-2005, 08:36 PM   #4
power123
LQ Newbie
 
Registered: Nov 2005
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 11-02-2005, 06:32 PM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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.
 
Old 11-02-2005, 06:36 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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().
 
Old 11-03-2005, 12:04 AM   #7
gxf
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C code fflush/fpurge/getchar problems boxerboy Programming 2 08-29-2005 02:46 PM
fsync() is slow on some systems brian_p_sully Linux - General 0 05-06-2004 10:58 AM
fflush fuction in c jetfreggel Programming 13 12-26-2002 07:02 PM


All times are GMT -5. The time now is 12:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration