Linux 是否保证在 close() 之后将文件的内容刷新到磁盘? [英] Does Linux guarantee the contents of a file is flushed to disc after close()?

查看:18
本文介绍了Linux 是否保证在 close() 之后将文件的内容刷新到磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 close()fclose() 关闭文件时(例如),Linux 是否保证将文件写回(永久)磁盘?

When a file is closed using close() or fclose() (for example), does Linux guarantee that the file is written back to (persistent) disc?

我的意思是,如果 close() 返回 0,然后立即断电,之前写入的数据是否保证持久,即持久?

What I mean is, if close() returns 0 and then immediately afterwards the power fails, are previously written data guaranteed to persist, i.e. be durable?

fsync() 系统调用确实提供了这种保证.关闭文件也足够了吗?

The fsync() system call does provide this guarantee. Is closing a file also sufficient?

目前我找不到任何可以以某种方式提出任何主张的内容.

I can't find anything which makes any claim one way or another at the moment.

问题 2:

如果 close() 确实隐式地执行了 fsync(),有没有办法告诉它不要这样做?

If close() does implicitly do an fsync(), is there a way of telling it not to?

推荐答案

来自man 2 close":

成功关闭并不能保证数据已经被成功保存到磁盘,作为内核推迟写入.

A successful close does not guarantee that the data has been successfully saved to disk, as the kernel defers writes.

手册页说如果你想确保你的数据在磁盘上,你必须自己使用 fsync().

The man page says that if you want to be sure that your data are on disk, you have to use fsync() yourself.

这篇关于Linux 是否保证在 close() 之后将文件的内容刷新到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆