强制将文件写入磁盘 [英] Force write of a file to disk

查看:845
本文介绍了强制将文件写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施ping / pong缓冲方案,以便将文件安全地写入磁盘。我在Linux / CentOS机器上使用C ++ / Boost。现在我面临的问题,强制实际写入文件到磁盘。是否可以这样做,不管文件系统(ext3 / ext4)/ SO自定义规则/ RAID控制器/硬盘控制器的所有缓存策略?

I'm currently implementing a ping/pong buffering scheme to safely write a file to disk. I'm using C++/Boost on a Linux/CentOS machine. Now I'm facing the problem to force the actual write of the file to disk. Is it possible to do so irrespective of all the caching policies of the filesystem (ext3/ext4) / SO custom rules / RAID controller / harddisk controller ?

使用纯fread()/ fwrite(),c ++ ostream或boost文件系统?

Is it best to use plain fread()/fwrite(), c++ ostream or boost filesystem?

我听说只是清空文件(fflush实际写

I've heard that simply flushing out the file (fflush()) doesn't guarantee the actual write

谢谢!

推荐答案

fflush(对于FILE *),std :: flush(对于IOStream)强制您的程序发送到操作系统。

fflush (for FILE*), std::flush (for IOStream) to force your program to send to the OS.

POSIX已


  • 同步(2)写入其缓冲区,但可以在写入完成之前返回(Linux正在等待数据在返回之前发送到硬件)。

  • sync(2) to ask to schedule writing its buffers, but can return before the writing is done (Linux is waiting that the data is send to the hardware before returning).

fsync它保证等待数据发送到硬件,但需要一个文件描述符(你可以从一个FILE *与fileno(3),我知道没有标准的方法从一个IOStream得到一个)。

fsync(2) which is guaranteed to wait for the data to be send to the hardware, but needs a file descriptor (you can get one from a FILE* with fileno(3), I know of no standard way to get one from an IOStream).

在所有情况下,硬件可能有自己的缓冲区(但是如果它有控制权,一个很好的实现将尝试刷新它们,并且ISTR有些磁盘使用电容器,使得它们能够刷新任何事情到电源)和网络文件系统都有自己的警告。

In all cases, the hardware may have it's own buffers (but if it has control on it, a good implementation will try to flush them also and ISTR that some disks are using capacitors so that they are able to flush whatever happens to the power) and network file systems have their own caveat.

这篇关于强制将文件写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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