数据写入磁盘回调
在 Linux 中数据成功写入磁盘后如何获得回调?
我希望将我的程序数据库文件映射到内存中以进行读/写操作,并在写入成功命中磁盘后接收回调。有点像旧的 VMS 过去所做的事情。
How can I get callbacks once data has been successfully written to disk in Linux?
I would like to have my programs db file mapped into memory for read/write operations and receive callbacks once a write has successfully hit disk. Kind of like what the old VMSs used to do..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用 fdatasync(如果您确实需要同步元数据,则调用 fsync)并等待它返回。
您可以从另一个线程执行此操作,但如果一个线程写入文件,而另一个线程正在执行 fdatasync(),则无法清楚哪些写入保证是持久的。
想要以保证持久的方式存储事务日志的数据库需要调用 fdatasync。
数据库(例如innodb)通常在其主数据文件上使用直接IO(以及它们自己的数据缓存,而不是依赖于操作系统),以便它们知道它将以可预测的方式写入。
You need to call fdatasync (or fsync if you really need the metadata to be synchronised as well) and wait for it to return.
You could do this from another thread, but if one thread writes to the file while another thread is doing a fdatasync(), it's not going to be clear which of the writes are guaranteed to be persistent or not.
Databases which want to store transaction logs in a guaranteed-durable way, need to call fdatasync.
Databases (such as innodb) typically use direct IO (as well as their own data-caching, rather than rely on the OS) on their main data files, so that they know that it will be written in a predictable manner.
据我所知,当文件(或 mmap 编辑区域)之间发生实际同步时,您无法收到任何通知,甚至文件的时间戳也不会改变。不过,您可以使用
fsync
强制同步文件(或区域)。也很难看出为什么你会想要这样。文件 IO 应该是不透明的。
As far as I know you cannot get any notification when the actual synchronization between a file (or a
mmap
ed region) happen, not even the timestamps of the file are going to change. You can, however, force the synchronization of the file (or region) by usingfsync
.It is also hard to see a reason for why you would want that. File IO is supposed to be opaque.