linux fsync函数,Unix_Linux

fsync()和fdatasync()是Unix/Linux系统中用于将文件或文件描述符的修改同步到持久存储设备的函数。fsync()会将文件及元数据的修改写入磁盘,而fdatasync()只同步数据,不强制更新元数据。调用fsync()并不确保目录条目已同步,可能需要额外的fsync()操作。这两个函数在返回前会等待设备确认传输完成。fdatasync()旨在减少不需要所有元数据同步的应用程序的磁盘活动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unix_Linux 的 fsync

fsync, fdatasync - 同步文件在内核态与存储设备

内容简介

#include int fsync(intfd);int fdatasync(intfd);

描述

fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) where that file resides. The call blocks until the device reports that the transfer has completed. It also flushes metadata information associated with the file (see stat(2)).

Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.

fdatasync() is similar to fsync(), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled. For example, changes to st_atime or st_mtime (respectively, time of last access and time of last modification; see stat(2)) do not not require flushing because they are not necessary for a subsequent data read to be handled correctly. On the other hand, a change to the file size (st_size, as made by say ftruncate(2)), would require a metadata flush.

The aim of fdatasync(2) is to reduce disk activity for applications that do not require all metadata to be synchronised with the disk.

返回值

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

错误

标签

描述

EBADF

fd is not a valid file descriptor open for writing.

EIO

An error occurred during synchronization.

EROFS, EINVAL

fd is bound to a special file which does not support synchronization.

注意

If the underlying hard disk has write caching enabled, then the data may not really be on permanent storage when fsync() / fdatasync() return.

When an ext2 file system is mounted with the sync option, directory entries are also implicitly synced by fsync().

On kernels before 2.4, fsync() on big files can be inefficient. An alternative might be to use the O_SYNC flag to open(2).

遵循于

POSIX.1-2001

另请参阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值