Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-06-2004, 10:58 AM   #1
LQ Newbie
Registered: Jan 2003
Posts: 8

Rep: Reputation: 0
fsync() is slow on some systems

I have code that writes to a specified file and after each line (around 300 bytes) is written it calls fsync() on the file to force the changes to disk. The file in question is fairly small, under 10MB. On some servers the program takes about 1 second to run with it writing about 1200 lines. On other systems though it takes around 70 seconds. If I take out the fsync at the end of each write then the program runs in under a second on all systems. All the servers are only a couple years old and are mostly idle. They are all different model Compaq/HP servers with various 2.4 kernel revisions (RedHat 7.3).

I assume there must be some configuration parameter somewhere on the servers that is different and causing the long delays since its the same program on all the boxes. Anyone have any suggestions where to look? Or any other theories to the cause of the time difference?

I'm pretty sure the size of the file is irrelevant, I modified my test program to rewrite the same line of the file over and over (so file was only 317 bytes big) and it took the same amount of time.

Here is my test program that times creating the file with and without fsync():

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

int createFile( char *fname, int doSync );

char *NAME1 = "slowsync1.txt";
char *NAME2 = "slowsync2.txt";
int NUMLINES = 1296;

int main( int argc, char *argv[] )

if ( createFile( NAME1, 0 ) < 0 ) {
printf( "ERROR: Couldn't create file1, aborting...\n" );
exit( 0 );

if ( createFile( NAME2, 1 ) < 0 ) {
printf( "ERROR: Couldn't create file2, aborting...\n" );
exit( 0 );

exit( 0 );

} // main

int createFile( char *fname, int doSync )
int fd;
int x;
struct timeval start, end, diff;

gettimeofday( &start, NULL );

fd = open( fname, O_CREAT | O_WRONLY , 0660 );
if ( fd < 0 ) {
printf( "ERROR: Can't open %s, errno=%d\n", fname, errno );
return( -1 );

for ( x = 0; x < NUMLINES; x++ )

if ( write( fd, LINE, strlen(LINE) ) < 0 ) {
printf( "ERROR: Can't write to %s, errno=%d\n", fname, errno );
return( -2 );

if ( doSync == 1 ) {
if ( fsync( fd ) < 0 ) {
printf( "ERROR: Can't fsync %s, errno=%d\n", fname, errno );
return( -3 );

close( fd );

gettimeofday( &end, NULL );
if ( end.tv_usec < start.tv_usec ) {
end.tv_usec += 1000000;

diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_usec = end.tv_usec - start.tv_usec;

printf( "%s created in %d.%06d seconds", fname, diff.tv_sec, diff.tv_usec);
if ( doSync == 1 )
printf( " with fsync\n" );
printf( " \n" );

return( 0 );

} // createFile


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
fflush and fsync? power123 Programming 6 11-03-2005 12:04 AM
in redhatlinux systems seems to be working very slow tvsskumar Linux - Newbie 2 06-15-2004 01:33 PM
Mandrake 9.1 SLOW after panel loads, it isn't my systems specs Tw3NTy0n3 Linux - Distributions 1 07-12-2003 09:29 PM
Mandrake 9.1 SLOW after panel loads, it isn't my systems specs Tw3NTy0n3 Linux - Newbie 13 07-07-2003 05:17 AM
Mandrake 9.1 SLOW after panel loads, it isn't my systems specs Tw3NTy0n3 Linux - General 2 07-01-2003 08:51 AM

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

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