8    File Synchronization

By default, UNIX systems read from and write to a buffer cache that is kept in memory, and avoid actually transferring data to disk until the buffer is full or until the application calls a synchronization function to flush the buffer cache. For general-purpose applications, this practice increases performance by avoiding the relatively slow mechanical process of writing to disk more often then necessary.

However, realtime applications sometimes require input and output operations created specifically to support realtime requirements for timeliness and predictability:

Synchronized I/O is useful when the integrity of data and files is critical to an application. Synchronized output assures that data that is written to a device is actually stored there. Synchronized input assures that data that is read from a device is a current image of data on that device.

Two levels of file synchronization are available:

Tru64 UNIX supports POSIX 1003.1b file synchronization for the UFS and AdvFS file systems, as described in this chapter. However, use of the UFS file system is recommended for better realtime performance.

8.1    How to Ensure Data or File Integrity

You can ensure data integrity or file integrity at specific times by using function calls, or you can set file descriptor flags to force automatic file synchronization for each read or write call associated with that file.

Use of synchronized I/O may degrade system performance; see Chapter 11.

8.1.1    Using Function Calls

You can choose to write to buffer cache as usual, and call functions explicitly when you want the program to flush the buffer to disk. For instance, you may want to use the buffer cache when a lot of I/O is occurring, and call these functions when activity slows down. Two functions are available:

Function Description
fdatasync Flushes modified data only from the buffer cache, providing operation completion with data integrity
fsync Flushes modified data and file control information from the buffer cache, providing operation completion with file integrity

See the online reference pages for a complete description of these functions.

8.1.2    Using File Descriptors

If you want to write data to disk in all cases automatically, you can set file descriptor flags to force this behavior instead of making explicit calls to fdatasync or fsync.

You can use the following file descriptor flags with the open or fcntl function:

Flag Description
O_DSYNC

Forces data synchronization for each write operation. Example:

fd = open("my_file", O_RDWR|O_CREAT|O_DSYNC, 0666);
 

O_SYNC

Forces file and data synchronization for each write operation. Example:

fd = open("my_file", O_RDWR|O_CREAT|O_SYNC, 0666);
 

O_RSYNC

When either of the other two flags is in effect, forces the same file synchronization level for each read as is in effect for each write. Use of O_RSYNC has no effect in the absence of O_DSYNC or O_SYNC. Examples:

fd = open("my_file", O_RDWR|O_CREAT|O_SYNC|O_RSYNC, 0666);
fd = open("my_file", O_RDWR|O_CREAT|O_DSYNC|O_RSYNC, 0666);

If both the O_DSYNC and O_SYNC flags are set using the open or fcntl function, O_SYNC takes precedence.

Restrictions