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 sync function to flush the buffer cache. This increases performance by avoiding the relatively slow mechanical process of writing to disk more often then necessary.
Realtime input and output operations are of two types:
Asynchronous I/O, which frees the application to perform other tasks while input is written or read (see Chapter 7)
Synchronized I/O, which performs the write or read operation and verifies its completion before returning
DIGITAL UNIX supports POSIX 1003.1b file synchronization for the UFS and AdvFS file systems, as described in this chapter. However, DIGITAL recommends use of the UFS file system for better realtime performance.
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, data integrity and file integrity:
Data integrity
Write operations: data in the buffer is transferred to disk, along with file system information necessary to retrieve the data.
Read operations: any pending write operations relevant to the data being read complete with data integrity before the read operation is performed.
File integrity
Write operations: data in the buffer and all file system information related to the operation are transferred to disk.
Read operations: any pending write operations relevant to the data being read complete with file integrity before the read operation is performed.
You can assure data integrity or file integrity at specific times by using
function calls, or you can set file status 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.
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 |
Refer to online reference pages for a complete description of these functions.
If you want to write data to disk in all cases automatically, you can set
file status flags to force this behavior instead of making explicit calls to
fdatasync
or
fsync
.
To set this behavior, use these 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 well as each write operation. 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.