ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

fsync和syncfs有什么区别?

2019-05-16 08:51:42  阅读:924  来源: 互联网

标签:filesystems posix c-3 linux unistd-h


fsync和syncfs有什么区别?

int syncfs(int fd);
int fsync(int fd);

fync的联机帮助页说明如下:

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 stor‐
age device) so that all changed information can be retrieved even after the system crashed or
was rebooted. This includes writing through or flushing a disk cache if present. The call
blocks until the device reports that the transfer has completed. It also flushes metadata
information associated with the file (see stat(2)).

syncfs的联机帮助页说明如下:

sync() causes all buffered modifications to file metadata and data to be written to the underly‐
ing filesystems.

syncfs() is like sync(), but synchronizes just the filesystem containing file referred to by the
open file descriptor fd.

对我来说两者似乎都是平等它们正在同步filedescriptor引用的文件和关联的元数据.

最佳答案:

首先,fsync()(和sync())是POSIX标准功能,而syncfs()是仅Linux.

因此可用性是一个很大的区别.

POSIX standard for fsync()开始:

The fsync() function shall request that all data for the open file
descriptor named by fildes is to be transferred to the storage
device associated with the file described by fildes. The nature of
the transfer is implementation-defined. The fsync() function shall
not return until the system has completed that action or until an
error is detected.

请注意,这只是一个请求.

the POSIX standard for sync()开始:

The sync() function shall cause all information in memory that
updates file systems to be scheduled for writing out to all file
systems.

The writing, although scheduled, is not necessarily complete upon
return from sync().

同样,这不是保证会发生的事情.

The Linux man page for syncfs() (and sync()) states

sync() causes all pending modifications to filesystem metadata and
cached file data to be written to the underlying filesystems.

syncfs() is like sync(), but synchronizes just the filesystem
containing file referred to by the open file descriptor fd.

请注意,当函数返回时未指定.

The Linux man page for fsync() states

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)
so that all changed information can be retrieved even if the system
crashes or is rebooted. This includes writing through or flushing a
disk cache if present. The call blocks until the device reports that
the transfer has completed.

As well as flushing the file data, fsync() also flushes the metadata
information associated with the file (see inode(7)).

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.

请注意,Linux为fsync()提供的保证比为sync()或syncfs()提供的保证要强得多,而对于fsync()和sync()则保留了POSIX.

综上所述:

> POSIX fsync():“请将此文件的数据写入磁盘”
> POSIX sync():“当你到达它时,将所有数据写入磁盘”
> Linux sync():“将所有数据写入磁盘(当你绕过它时?)”
> Linux syncfs():“将与此文件关联的文件系统的所有数据写入磁盘(当你接触它时?)”
> Linux fsync():“将此文件的所有数据和元数据写入磁盘,直到执行后才返回”

请注意,Linux手册页未指定sync()和syncfs()何时返回.

标签:filesystems,posix,c-3,linux,unistd-h
来源: https://codeday.me/bug/20190516/1113234.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有