Name

fsync - synchronizes a file's complete in-core state with that on disk

Library

libc.lib

Synopsis

  #include <unistd.h>
  int fsync (int fd);

Return values


.Rv -std fsync

Detailed description

The fsync system call causes all modified data and attributes of fd to be moved to a permanent storage device. This normally results in all in-core modified copies of buffers for the associated file to be written to a disk.

The fsync system call should be used by programs that require a file to be in a known state, for example, in building a simple transaction facility.


Examples

/**
  * Detailed description : Simple usage of fsync system call.
  * Preconditions : Example.txt if present should not a ready-only file
**/
#include <unistd.h>
#include <fcntl.h>
int main()
{
  int fd = 0;
  fd = open("Example.txt" , O_CREAT | O_RDWR , 0666);
  if(fd < 0 )  
  {
     printf("Failed to open file Example.txt \n");
     return -1;
  }
  if(fsync(fd) < 0 )  
  {
     printf("fsync system call failed \n");
     return -1;
  }
  close(fd);
  printf("fsync system call succeeded \n");
  return 0;
}

         

Output

fsync system call succeeded

         


Errors

The fsync fails if:
[EBADF]
  The fd argument is not a valid descriptor.
[EINVAL]
  The fd argument refers to a socket, not to a file.
[EIO] An I/O error occurred while reading from or writing to the file system.

See also

sync, syncer, sync
The fsync system call appeared in BSD 4.2.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top