(standard c libraries translation )sync

sync, syncfs - commit buffer cache to disk
sync, syncfs - 提交buffer缓冲到磁盘

所需头文件
#include <unistd.h>

void sync(void);

void syncfs(int fd);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

sync():
   _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

syncfs():
   _GNU_SOURCE

sync() causes all buffered modifications to file metadata and data to be written to the underlying file systems.
sync会把所有缓冲区的修改提交到文件元数据和文件系统
syncfs() is like sync(), but synchronizes just the file system containing file referred to by the open file descriptor fd.
syncfd类似与sync,但是仅仅同步和文件描述符fd相关的数据

syncfs() returns 0 on success; on error, it returns -1 and sets errno to indicate the error.
syncfs成功的时候返回0,失败的时候返回-1,errno被设置成代表错误的值
sync() is always successful.
sync()只返回成功

syncfs() can fail for at least the following reason:
EBADF  fd is not a valid file descriptor.
fd不是一个合法的文件描述符

syncfs() first appeared in Linux 2.6.39.
syncfs第一次是出现在2.6.39中

Since  glibc  2.2.2  the  Linux  prototype for sync() is as listed above, following the various standards.  In libc4, libc5, and glibc up to 2.2.1 it was "int sync(void)", and sync() always returned 0.
自从glibc2.2.2,linux sync的原型跟据不同的标准就如上所述,在libc4,libc5和glibc2.2.1,是“int sync(void)”,sync只会返回0

According to the standard specification (e.g., POSIX.1-2001), sync() schedules the writes, but may return before the actual writing is done. However, since version 1.3.20 Linux does actually wait.  (This still does not guarantee data integrity: modern disks have large caches.)
通过标准的规定,sync规定写,但是可能在实际写完成之前就返回了,自从linux1.3.20之后,sync会等待。


fsync, fdatasync - synchronize a file's in-core state with storage device
fsync, fdatasync - 同步文件的内部状态到存储设备

所需头文件
#include <unistd.h>

int fsync(int fd);

int fdatasync(int fd);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

fsync(): _BSD_SOURCE || _XOPEN_SOURCE || /* since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
fdatasync(): _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500

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)).
fsync转换文件描述符所关联的文件的内部改动到文件所在的磁盘设备,调用将阻塞直到转换完成,同样冲刷文件所关联的元数据信息

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.
调用fsync并不能确保文件内容已经写入到磁盘,因此显式的调用fd的fsync是有必要的

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 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.
fdatasync类似于fsync,但是不冲刷修改的元数据,除非元数据是为了后续调用所能正常使用的,例如,改动st_atime和st_mtime不会要求冲刷数据,因为他们对于后续的读操作不是不许的,另一方面,修改文件的大小,需要元数据冲刷

The aim of fdatasync() is to reduce disk activity for applications that do not require all metadata to be synchronized with the disk.
fdatasync是为了减少应用程序导致的磁盘活动,不需要所有的元数据同步到磁盘

On success, these system calls return zero.  On error, -1 is returned, and errno is set appropriately.
成功的时候这些调用返回0,失败的时候返回-1,errno被设置成适当的值

EBADF  fd is not a valid file descriptor open for writing.
fd不是一个合法可写的文件描述符
EIO    An error occurred during synchronization.
同步的时候发生了错误
EROFS, EINVAL fd is bound to a special file which does not support synchronization.
fd限定了一个特殊文件,不支持同步
On  POSIX  systems  on which fdatasync() is available, _POSIX_SYNCHRONIZED_IO is defined in <unistd.h> to a value greater than 0.  (See also sysconf(3).)
在POSIX系统中fdatasync是可用的,_POSIX_SYNCHRONIZED_IO定义在<unistd.h>中的一个大于0的值

Applications that access databases or log files often write a tiny data fragment (e.g., one line in a log file) and then call fsync()  immediately  in  order  to  ensure  that the written data is physically stored on the hard disk.  Unfortunately, fsync() will always initiate two write operations: one for the newly written data and another one in order to update the modification time stored in the inode.  If the modification time is not a part of the transaction concept fdatasync() can be used to avoid unnecessary inode disk write operations.
应用程序接触数据库或者日志文件经常写一个小的数据片(例如日志从的一行),然后调用直接调用fsync来确保写数据被写入到物理磁盘中,不幸的是,fsync会初始化两个写操作,一个是新写入的数据,另一个是更新inode的修改时间,如果修改时间不是需要转换的一部分,那么fdatasync可以用来避免不必要的inde磁盘写操作
If  the  underlying  hard  disk  has  write caching enabled, then the data may not really be on permanent storage when fsync() / fdatasync() return.
如果后台的磁盘写缓冲是打开的,那么fsync和fdatasync返回的时候并不代表数据已经实际写入
When an ext2 file system is mounted with the sync option, directory entries are also implicitly synced by fsync().
如果ext2文件系统以sync选项挂在,路径条目需要使用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).
在kernel2.4之前,fsync一个大的文件可能会失效,一个可选的方案是使用O_SYNC标志打开
In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and so has no performance advantage.
在linux2.2之前,fdatasync跟fsync是相同的,所以没有任何性能提升
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值