In most modern operating systems the normal I/O operations are not
executed synchronously. I.e., even if a write
system call
returns this does not mean the data is actually written to the media,
e.g., the disk.
In situations where synchronization points are necessary,you can use special functions which ensure that all operations finish before they return.
A prototype for sync
can be found in `unistd.h'.
The return value is zero to indicate no error.
Programs more often want to ensure that data written to a given file is
committed, rather than all data in the system. For this, sync
is overkill.
fsync
can be used to make sure all data associated with the
open file fildes is written to the device associated with the
descriptor. The function call does not return unless all actions have
finished.
A prototype for fsync
can be found in `unistd.h'.
This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time fsync
is
called. If the thread gets cancelled these resources stay allocated
until the program ends. To avoid this, calls to fsync
should be
protected using cancellation handlers.
The return value of the function is zero if no error occurred. Otherwise it is @math{-1} and the global variable errno is set to the following values:
EBADF
EINVAL
Sometimes it is not even necessary to write all data associated with a file descriptor. E.g., in database files which do not change in size it is enough to write all the file content data to the device. Meta-information like the modification time etc. are not that important and leaving such information uncommitted does not prevent a successful recovering of the file in case of a problem.
fdatasync
function returns, it is ensured
that all of the file data is written to the device. For all pending I/O
operations, the parts guaranteeing data integrity finished.
Not all systems implement the fdatasync
operation. On systems
missing this functionality fdatasync
is emulated by a call to
fsync
since the performed actions are a superset of those
required by fdatasyn
.
The prototype for fdatasync
is in `unistd.h'.
The return value of the function is zero if no error occurred. Otherwise it is @math{-1} and the global variable errno is set to the following values:
EBADF
EINVAL
Go to the first, previous, next, last section, table of contents.