简体   繁体   English

flush、write() 和 fflush() 之间的实际区别是什么?

[英]what is practical differences between flush, write() and fflush()?

In this post , the answer said这篇文章中,答案是

Flushing: To sync the temporary state of your application data with the permanent state of the data (in a database, or on disk). Flushing:将应用程序数据的临时 state 与数据的永久 state 同步(在数据库中或磁盘上)。

I think that the flush is executed when some buffer is written to an i/o device (like disk) by the write() system call.我认为刷新是在write()系统调用将某个缓冲区写入 i/o 设备(如磁盘)时执行的。

So it seems that a data writing to a device with write() and the data flushing to the device are to do the same things.因此,似乎使用write()将数据写入设备和将数据刷新到设备是做同样的事情。

If so, can I say that the flushing a data with fflush() and the writing the data with write() are completely same?如果是这样,我可以说用fflush()刷新数据和用write()写入数据完全相同吗?

First, let's do the obvious thing:首先,让我们做一件显而易见的事情:

fflush冲洗

For output streams (and for update streams on which the last operation was output), writes any unwritten data from the stream's buffer to the associated output device.对于 output 流(以及输出最后一个操作的更新流),将流缓冲区中的任何未写入数据写入关联的 output 设备。

The C Standard doesn't state how the data is written to the output device. C 标准没有 state 数据是如何写入 output 设备的。 On Posix systems, most likely via write , other systems might have different (similar) interfaces.在 Posix 系统上,很可能通过write ,其他系统可能有不同(相似)的接口。

Conceptually speaking, a flush will use the underlying write primitive to transmit the data from the buffer to the output device.从概念上讲,刷新将使用底层写原语将数据从缓冲区传输到 output 设备。

In short:简而言之:

  • fflush() the same as write() -> No. fflush()write()相同 -> 否。
  • fflush() uses write() -> Yes, most likely. fflush()使用write() -> 是的,很有可能。
  • fflush() and write() ensures the data to be written to the output device -> Yes. fflush()write()确保数据写入 output 设备 -> 是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM