fsync()--Synchronize Changes to File


  Syntax
 #include <unistd.h>

 int fsync(int file_descriptor);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The fsync() function transfers all data for the file indicated by the open file descriptor file_descriptor to the storage device associated with file_descriptor. fsync() does not return until the transfer is complete, or until an error is detected.


Parameters

file_descriptor
(Input) The file descriptor of the file that is to have its modified data written to permanent storage.

Authorities

No authorization is required. Authorization is verified during open() or creat().


Return Value

0
fsync() was successful.
-1
fsync() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If fsync() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

If interaction with a file server is required to access the object, errno could indicate one of the following errors:



Error Messages

The following messages may be sent from this function:


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:

    • Where multiple threads exist in the job.
    • The object on which this function is operating resides in a file system that is not threadsafe. Only the following file systems are threadsafe for this function:

      • "Root" (/)
      • QOpenSys
      • User-defined
      • QNTC
      • QSYS.LIB
      • Independent ASP QSYS.LIB
      • QOPT
      • Network File System
      • QFileSvr.400

  2. Using this function on a character special file will result in a return value of -1 and the errno global value set to EINVAL.

Related Information


Example

The following example uses fsync().

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

#define mega_string_len 250000

main() {
  char *mega_string;
  int  file_descriptor;
  int  ret;
  char fn[]="fsync.file";

  if ((mega_string = (char*) malloc(mega_string_len)) == NULL)
    perror("malloc() error");
  else if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    memset(mega_string, 's', mega_string_len);
    if ((ret = write(file_descriptor,
                     mega_string, mega_string_len)) == -1)
      perror("write() error");
    else {
      printf("write() wrote %d bytes\n", ret);
      if (fsync(file_descriptor) != 0)
        perror("fsync() error");
      else if ((ret = write(file_descriptor,
                            mega_string, mega_string_len)) == -1)
        perror("write() error");
      else
        printf("write() wrote %d bytes\n", ret);
    }
    close(file_descriptor);
    unlink(fn);
  }
  free(mega_string);
}

Output:

write() wrote 250000 bytes
write() wrote 250000 bytes


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]