数据写入磁盘回调

发布于 2024-12-11 16:13:28 字数 120 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-12-18 16:13:28

您需要调用 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.

我只土不豪 2024-12-18 16:13:28

据我所知,当文件(或 mmap 编辑区域)之间发生实际同步时,您无法收到任何通知,甚至文件的时间戳也不会改变。不过,您可以使用 fsync 强制同步文件(或区域)。

也很难看出为什么你会想要这样。文件 IO 应该是不透明的。

As far as I know you cannot get any notification when the actual synchronization between a file (or a mmaped 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 using fsync.

It is also hard to see a reason for why you would want that. File IO is supposed to be opaque.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文