$linuxjunkies
>

O_SYNC

also: synchronous write flag

A file opening flag that forces all writes to be synchronously committed to disk immediately, ensuring data is physically written before the write call returns.

O_SYNC is a flag used with the open() system call that guarantees data durability by blocking write operations until data is actually written to the storage device. Without this flag, the kernel may cache writes in memory for performance reasons.

When O_SYNC is set, every write() call waits for the data to be written to disk and file metadata (like modification time) to be updated before returning control to the program. This eliminates the risk of data loss from system crashes or power failures, but at the cost of reduced performance.

Example: int fd = open("/var/log/critical.log", O_WRONLY | O_SYNC); opens a file for synchronous writing, ensuring each log entry is immediately persisted to disk.

Related flags include O_DSYNC (syncs only data, not metadata) and O_RSYNC (synchronizes reads as well). O_SYNC is commonly used for critical data like database transactions and application logs where data loss is unacceptable.

Related terms