写系统调用直接写数据到磁盘? [英] Write system call writes data to disk directly?
问题描述
我已经阅读了几个与此相关的问题(此处),但我仍然有一些困惑。
我的理解是,写入系统调用将数据放入缓冲区缓存
(操作系统缓存为在这个问题中提到)。当 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 theBuffered 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 awrite
system call issued to move the contents toBuffered Cache
. If I usefflush
then data related to this particular file that is present in theC RTL buffers
as well asBuffered 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 thewrite
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 bothfflush
and the low-levelfsync
.Edit: Answer rephrased.
这篇关于写系统调用直接写数据到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!