Node:Synchronizing I/O, Next:Asynchronous I/O, Previous:Waiting for I/O, Up:Low-Level I/O
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.
int sync (void) | Function |
A call to this function will not return as long as there is data which
has not been written to the device. All dirty buffers in the kernel will
be written and so an overall consistent system can be achieved (if no
other process in parallel writes data).
A prototype for 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.
int fsync (int fildes) | Function |
The fsync function 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 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 The return value of the function is zero if no error occurred. Otherwise it is -1 and the global variable errno is set to the following values:
|
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.
int fdatasync (int fildes) | Function |
When a call to the 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 The prototype for The return value of the function is zero if no error occurred. Otherwise it is -1 and the global variable errno is set to the following values:
|