如何知道何时需要缓冲区刷新?
How to know when a buffer flush is needed?
考虑两个刷新缓冲区的函数:
fflush()
sync()
我怎么知道什么时候需要调用其中一个?
我知道在printf()
中添加一个'\n'
会刷新输出缓冲区,但是如果字符串不包含这样的字符,我什么时候可以跳过这个调用,什么时候不调用(多线程系统?)?
sync
也是如此。我有一个将文件保存到文件系统的功能(保存是通过一系列系统调用完成的),似乎在特定情况下没有调用 sync
文件不会保存
不幸的是,我目前没有关于该案例的所有详细信息[我所知道的是文件已保存,之后会立即关闭电源(不要' 确切知道多久)并且文件在重新启动后不存在 ]。在我 运行 的所有测试中,文件都已正确保存。
那么,我如何确定系统何时会刷新文件 data/metadata 缓冲区以及何时不会,我需要显式调用 sync()
?
引用 man (未指定何时需要显式调用):
sync, syncfs - commit buffer cache to disk
sync() causes all buffered modifications to file metadata and data to be written to the underlying file systems.
fflush - flush a stream
For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.
附带问题:
The 1st comment on this answer 展示了一种使用 setbuf(stdout, NULL);
禁用 stdout
缓冲的方法。有没有类似 sync
的东西(那个缓冲区用的是什么?)?
sync()
如果您使用文件流而不是文件描述符,您很少需要调用 sync()
. When you do, you have something that it is crucial must be recorded on disk ASAP. However, sync()
will return having scheduled the writing of buffers in kernel memory, not after they've been written, so you won't know that they've actually been written — so it isn't wholly reliable. If you need more control over the writing for your file, look at the O_SYNC
, O_DSYNC
, O_RSYNC
flags for open()
. You will probably have to use fcntl()
and fileno()
来设置这些标志。
两个注意事项:
sync()
不会将您的进程(或任何其他进程)的缓冲区写入内核缓冲池;它与 fflush()
. 完全无关
sync()
影响系统上所有进程写入的所有数据——如果您的应用程序经常使用它,您可能会变得不受欢迎;它颠覆了内核缓存数据的良好工作。
fflush()
fflush()
函数确保数据已从您的应用程序缓冲区写入内核缓冲池(对于单个文件,或者对于所有输出文件,如果您使用 fflush(0)
或 fflush(NULL)
).它不直接影响其他进程。同样,当您需要确信挂起的输出已发送到内核以进行后续传输时,您可以使用它。您可以使用它的一个地方是在您希望提示出现的输入操作之前,即使它最后没有换行符。否则,您不经常使用它,但您可以在任何时候使用它来确保数据已发送到内核进行写入。如果您正在调试并且您的程序崩溃了,少量的 fflush()
语句可以确保在崩溃之前写入挂起的输出。这可以帮助更准确地揭示问题所在(但使用调试器也可以)。
请注意,设置无缓冲输出 (setbuf(stdout, NULL)
or setvbuf(stdout, NULL, _IONBF, 0)
) 意味着所有输出都会发生 'immediately'。这不一定有利于性能。您有时会使用它,但很少使用。
考虑两个刷新缓冲区的函数:
fflush()
sync()
我怎么知道什么时候需要调用其中一个?
我知道在printf()
中添加一个'\n'
会刷新输出缓冲区,但是如果字符串不包含这样的字符,我什么时候可以跳过这个调用,什么时候不调用(多线程系统?)?
sync
也是如此。我有一个将文件保存到文件系统的功能(保存是通过一系列系统调用完成的),似乎在特定情况下没有调用 sync
文件不会保存
不幸的是,我目前没有关于该案例的所有详细信息[我所知道的是文件已保存,之后会立即关闭电源(不要' 确切知道多久)并且文件在重新启动后不存在 ]。在我 运行 的所有测试中,文件都已正确保存。
那么,我如何确定系统何时会刷新文件 data/metadata 缓冲区以及何时不会,我需要显式调用 sync()
?
引用 man (未指定何时需要显式调用):
sync, syncfs - commit buffer cache to disk
sync() causes all buffered modifications to file metadata and data to be written to the underlying file systems.
fflush - flush a stream
For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.
附带问题:
The 1st comment on this answer 展示了一种使用 setbuf(stdout, NULL);
禁用 stdout
缓冲的方法。有没有类似 sync
的东西(那个缓冲区用的是什么?)?
sync()
如果您使用文件流而不是文件描述符,您很少需要调用 sync()
. When you do, you have something that it is crucial must be recorded on disk ASAP. However, sync()
will return having scheduled the writing of buffers in kernel memory, not after they've been written, so you won't know that they've actually been written — so it isn't wholly reliable. If you need more control over the writing for your file, look at the O_SYNC
, O_DSYNC
, O_RSYNC
flags for open()
. You will probably have to use fcntl()
and fileno()
来设置这些标志。
两个注意事项:
sync()
不会将您的进程(或任何其他进程)的缓冲区写入内核缓冲池;它与fflush()
. 完全无关
sync()
影响系统上所有进程写入的所有数据——如果您的应用程序经常使用它,您可能会变得不受欢迎;它颠覆了内核缓存数据的良好工作。
fflush()
fflush()
函数确保数据已从您的应用程序缓冲区写入内核缓冲池(对于单个文件,或者对于所有输出文件,如果您使用 fflush(0)
或 fflush(NULL)
).它不直接影响其他进程。同样,当您需要确信挂起的输出已发送到内核以进行后续传输时,您可以使用它。您可以使用它的一个地方是在您希望提示出现的输入操作之前,即使它最后没有换行符。否则,您不经常使用它,但您可以在任何时候使用它来确保数据已发送到内核进行写入。如果您正在调试并且您的程序崩溃了,少量的 fflush()
语句可以确保在崩溃之前写入挂起的输出。这可以帮助更准确地揭示问题所在(但使用调试器也可以)。
请注意,设置无缓冲输出 (setbuf(stdout, NULL)
or setvbuf(stdout, NULL, _IONBF, 0)
) 意味着所有输出都会发生 'immediately'。这不一定有利于性能。您有时会使用它,但很少使用。