Chapter Contents

Previous

Next
fsync

fsync



Flush UNIX style File Buffers to Disk

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTIONS
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <fcntl.h>

int fsync(int fn);

The synopsis for the POSIX implementation is

#include <unistd.h>

int fsync(int fn);

You may use either header file in your program.


DESCRIPTION

The fsync function flushes the output buffers to disk for the UNIX style file whose file number is fn . The fsync function also performs additional system-dependent operations to ensure that the data are accessible later, even if the program or the system fails later. The file position is unchanged. fsync returns when the output buffers are flushed or when an error is detected.


RETURN VALUE

The fsync function returns 0, or EOF if an error occurs.


CAUTIONS

Using fsync is expensive for files that are processed using a temporary copy because the file's entire contents must be copied each time fsync is called.


EXAMPLE

#include <fcntl.h>

extern int num_updates;
extern int fd;
int rc;

if (num_updates == 100) {
   rc = fsync(fd);            /* Flush updates to disk. */
   if (rc != 0) {
      puts("Error saving recent updates.");
      close(fd);
      abort();
   }
   num_updates = 0;           /* Reset update counter.  */
}


RELATED FUNCTIONS

afflush


SEE ALSO

I/O Functions


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.