免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3539 | 回复: 9
打印 上一主题 下一主题

sync 能确保数据写到磁盘上吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-26 19:11 |只看该作者 |倒序浏览
Kernle:
2.6.16.60-0.21-bigsmp #1 SMP Tue May 6 12:41:02 UTC 2008 i686 i686 i386 GNU/Linux

论坛徽章:
0
2 [报告]
发表于 2010-07-26 20:10 |只看该作者
sync 能确保数据写到磁盘上吗?

不能,有可能失败,比如磁盘已满

论坛徽章:
0
3 [报告]
发表于 2010-07-26 20:31 |只看该作者
假如不考虑磁盘满和磁盘损坏等情况, 调用完sync 后, 机器立刻断电呢?

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
4 [报告]
发表于 2010-07-26 21:04 |只看该作者
sync会进行磁盘IO,这需要一段时间
如果立刻断电,这个会比较麻烦
LZ可以试验一下,整个机器,运行一些程序,使得系统负载较大
然后sync看一下大致需要多长时间结束

论坛徽章:
0
5 [报告]
发表于 2010-07-27 19:04 |只看该作者
回复 4# openspace


    sync 只是同步原语,保证cache和hd上数据同步有很多技术,也有很多分析,日志、barrier等等。内核也无法保证数据的绝对正确,因此,谁也无法提供保证,最多就是做成‘事务’型的。譬如数据库。

论坛徽章:
0
6 [报告]
发表于 2010-07-28 00:34 |只看该作者
如果加上机器立刻断电,那慢速I/O都不能确保正确了

论坛徽章:
0
7 [报告]
发表于 2010-07-28 08:52 |只看该作者
sync 调用结束后,即返回之后,是否只能保证数据已经同步到磁盘设备上(可能是磁盘缓冲区,也可能是硬盘上)?

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
8 [报告]
发表于 2010-07-28 09:55 |只看该作者
回复 7# river617


   
15.4.1. The sync ( ) System Call

The service routine sys_sync( ) of the sync( ) system call invokes a series of auxiliary functions:

    wakeup_bdflush(0);
    sync_inodes(0);
    sync_supers( );
    sync_filesystems(0);
    sync_filesystems(1);
    sync_inodes(1);


As described in the previous section, wakeup_bdflush( ) starts a pdflush kernel thread, which flushes to disk all dirty pages contained in the page cache.

The sync_inodes( ) function scans the list of superblocks looking for dirty inodes to be flushed; it acts on a wait parameter that specifies whether it must wait until flushing has been performed or not. The function scans the superblocks of all currently mounted filesystems; for each superblock containing dirty inodes, sync_inodes( ) first invokes sync_sb_inodes( ) to flush the corresponding dirty pages (we described this function earlier in the section "Looking for Dirty Pages To Be Flushed", then invokes sync_blockdev( ) to explicitly flush the dirty buffer pages owned by the block device that includes the superblock. This is done because the write_inode superblock method of many disk-based filesystems simply marks the block buffer corresponding to the disk inode as dirty; the sync_blockdev( ) function makes sure that the updates made by sync_sb_inodes( ) are effectively written to disk.

The sync_supers( ) function writes the dirty superblocks to disk, if necessary, by using the proper write_super superblock operations. Finally, the sync_filesystems( ) executes the sync_fs superblock method for all writable filesystems. This method is simply a hook offered to a filesystem in case it needs to perform some peculiar operation at each sync; this method is only used by journaling filesystems such as Ext3 (see Chapter 1.

Notice that sync_inodes( ) and sync_filesystems( ) are invoked twice, once with the wait parameter equal to 0 and the second time with the parameter equal to 1. This is done on purpose: first, they quickly flush to disk the unlocked inodes; next, they wait for each locked inode to become unlocked and finish writing them one by one.


这是从ULK3中找的
具体实现得看操作系统是怎么做的
注意最后一段的描述

论坛徽章:
0
9 [报告]
发表于 2010-07-28 12:50 |只看该作者
sync 调用结束后,即返回之后,是否只能保证数据已经同步到磁盘设备上(可能是磁盘缓冲区,也可能是硬盘上) ...
river617 发表于 2010-07-28 08:52



         调用结束后,即返回之后,会强制数据 同步到磁盘设备上

论坛徽章:
0
10 [报告]
发表于 2010-07-28 14:12 |只看该作者
系统一级的缓存比如write会同步到磁盘,但是高级io缓存不会.比如fprintf, fwrite之类的不会同步到磁盘.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP