fsync - synchronize changes to a file
int fsync(int fildes);
The fsync() function moves all modified data and attributes
of the file descriptor fildes to a storage device. When
fsync() returns, all in-memory modified copies of buffers
associated with fildes have been written to the physical
medium. The fsync() function is different from sync(), which
schedules disk I/O for all files but returns before the I/O
completes. The fsync() function forces all outstanding data
operations to synchronized file integrity completion (see
fcntl(3HEAD) definition of O_SYNC.)
The fsync() function forces all currently queued I/O opera-
tions associated with the file indicated by the file
descriptor fildes to the synchronized I/O completion state.
All I/O operations are completed as defined for synchronized
I/O file integrity completion.
Upon successful completion, 0 is returned. Otherwise, -1 is
returned and errno is set to indicate the error. If the
fsync() function fails, outstanding I/O operations are not
guaranteed to have been completed.
The fsync() function will fail if:
EBADF The fildes argument is not a valid file descriptor.
EINTR A signal was caught during execution of the fsync()
EIO An I/O error occurred while reading from or writing to
the file system.
There was no free space remaining on the device con-
taining the file.
Remote connection timed out. This occurs when the file
is on an NFS file system mounted with the soft
option. See mount_nfs(1M).
In the event that any of the queued I/O operations fail,
fsync() returns the error conditions defined for read(2) and
The fsync() function should be used by applications that
require that a file be in a known state. For example, an
application that contains a simple transaction facility
might use fsync() to ensure that all changes to a file or
files caused by a given transaction were recorded on a
The manner in which the data reach the physical medium
depends on both implementation and hardware. The fsync()
function returns when notified by the device driver that the
write has taken place.
When a UFS file system is mounted with logging enabled, file
system transactions that free blocks from files might not
actually add those freed blocks to the file system's free
list until some unspecified time in the future. This
behavior improves file system performance but does not con-
form to the POSIX, Single UNIX Specification, SPARC Confor-
mance Definition, System V Application Binary Interface,
System V Interface Definition, and X/Open Portability Guide
Standards, which require that freed space be available
immediately. To enable standards conformance regarding file
deletions or to address the problem of not being able to
grow files on a relatively full UFS file system even after
files have been deleted, disable UFS logging (see
See attributes(5) for descriptions of the following attri-
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
| MT-Level | Async-Signal-Safe |
mount_nfs(1M), mount_ufs(1M), read(2), sync(2), write(2),
fcntl(3HEAD), fdatasync(3RT), attributes(5)
Man(1) output converted with