写系统调用直接写数据到磁盘? [英] Write system call writes data to disk directly?

查看:146
本文介绍了写系统调用直接写数据到磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几个与此相关的问题(此处),但我仍然有一些困惑。

我的理解是,写入系统调用将数据放入缓冲区缓存(操作系统缓存为在这个问题中提到)。当 Buffered Cache 变满时,它被写入磁盘。



缓冲IO在此之上进一步优化。它缓存在 C RTL缓冲区中,当它们满载时发出一个 write 系统调用来将内容移动到 Buffered Cache 。如果我使用 fflush ,那么与 C RTL缓冲区以及 Buffered Cache 被发送到磁盘。



我的理解是否正确?

解决方案

如何刷新stdio缓冲区取决于您使用的标准C库。引用 Linux手册页面


请注意,fflush()仅刷新C库提供的用户空间缓冲区。
为确保数据在物理上存储在磁盘上,内核缓冲区也必须被刷新
,例如sync(2)或fsync(2)。
blockquote>

这意味着在Linux系统上,使用 fflush 或溢出缓冲区将调用函数。但操作系统可能会保留内部缓冲区,而不是实际将数据写入设备。为确保数据真正写入设备,请同时使用 fflush 和低级 fsync



编辑:回答改正。


I've read couple of questions(here) related to this but I still have some confusion.

My understanding is that write system call puts the data into Buffered Cache(OS caches as referred in that question). When the Buffered Cache gets full it is written to the disk.

Buffered IO is further optimization on top of this. It caches in the C RTL buffers and when they get full a write system call issued to move the contents to Buffered Cache. If I use fflush then data related to this particular file that is present in the C RTL buffers as well as Buffered Cache is sent to the disk.

Is my understanding correct?

解决方案

How the stdio buffers are flushed is depending on the standard C library you use. To quote from the Linux manual page:

Note that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

This means that on a Linux system, using fflush or overflowing the buffer will call the write function. But the operating system may keep internal buffers, and not actually write the data to the device. To make sure the data is truly written to the device, use both fflush and the low-level fsync.

Edit: Answer rephrased.

这篇关于写系统调用直接写数据到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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