在Linux中写入后崩溃 [英] Crash after write in linux
问题描述
如果我使用write()将一些数据写入磁盘上的文件,将会发生什么.但是我的应用程序在刷新之前崩溃了.如果没有系统故障,是否可以保证我的数据最终被刷新到磁盘上?
What would happen if I were to use write() to write some data to a file on disk. But my application were to crash before flushing. Is it guaranteed that my data will get eventually flushed to disk if there is no system failure?
推荐答案
如果您使用的是 write
(而不是 fwrite
或 std :: ostream::write
),那么就没有进程缓冲.如果没有系统故障,那么数据迟早会(通常很快就会)写入磁盘.
If you're using write
(and not fwrite
or std::ostream::write
),
then there is no in process buffering. If there is no system failure,
then the data will, sooner or later (and generally fairly soon) be
written to disk.
如果您真的很担心数据完整性,可以或在标志中打开文件时,将 O_DSYNC
和 O_SYNC
标记.如果你这样做这样,可以确保将数据物理写入磁盘在从 write
返回之前.
If you're really concerned by data integrity, you can or in the flags
O_DSYNC
and O_SYNC
to the flags when you open the file. If you do
this, you are guaranteed that the data is physically written to the disk
before the return from write
.
这篇关于在Linux中写入后崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!