gtpc2m3eC/C++ Language Support User's Guide

fsync-Write Changes to Direct Access Storage

This function writes changes to direct-access storage.

Format

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

fildes
An open file descriptor for the file to be synchronized.

This function transfers all data for the file indicated by the open file descriptor, fildes, to the storage device associated with fildes. The fsync function does not return until the transfer has been completed or an error is detected.

Normal Return

If successful, the fsync function returns a 0 value.

Error Return

If unsuccessful, the fsync function returns -1 and sets errno to one of the following:

EBADF
fildes is not a valid open file descriptor.

EINVAL
The file cannot be synchronized.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Examples

The following example shows one use of the fsync function.

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
 
#define mega_string_len 250000
 
main() {
  char *mega_string;
  int  fd, ret;
  char fn[]="FSYNC.FILE";
 
  if ((mega_string = malloc(mega_string_len)) == NULL)
    perror("malloc() error");
  else if ((fd = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    memset(mega_string, 's', mega_string_len);
    if ((ret = write(fd, mega_string, mega_string_len)) == -1)
      perror("write() error");
    else {
      printf("write() wrote %d bytes\n", ret);
      if (fsync(fd) != 0)
        perror("fsync() error");
      else if ((ret = write(fd, mega_string, mega_string_len)) == -1)
        perror("write() error");
      else
        printf("write() wrote %d bytes\n", ret);
    }
    close(fd);
    unlink(fn);
  }
}

Output

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

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.