当应用程序崩溃时,如何行为linux缓冲区缓存? [英] How does behave linux buffer cache when an application is crashing down?

查看:513
本文介绍了当应用程序崩溃时,如何行为linux缓冲区缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用c ++文件流异步。我的意思是从不使用std :: flush也不std :: endl。我的应用程序写入大量数据到文件,突然崩溃。
缓存系统中剩余的数据是否刷新到磁盘,或者丢弃(丢失)?

Let's say I am using c++ files stream asynchronously. I mean never using std::flush nor std::endl. My application writes a lot of data to a file and abruptly crashes down. Is the data remaining in the cache system flushed to the disk, or discarded (and lost)?

推荐答案

这个问题是有多个'caches'在玩。

Complicating this problem is that there are multiple 'caches' in play.

C ++流有自己的内部缓冲机制。 Streams不要求操作系统写入磁盘,直到(a)您已经向缓冲区发送了足够的数据,流库认为写入不会被浪费(b)您特别要求刷新(c)流处于行缓冲模式,并且您沿着 endl 发送。当程序崩溃时,这些缓冲区中的任何数据都会丢失。

C++ streams have their own internal buffering mechanism. Streams don't ask the OS to write to disk until either (a) you've sent enough data into the buffer that the streams library thinks the write wouldn't be wasted (b) you ask for a flush specifically (c) the stream is in line-buffering mode, and you've sent along the endl. Any data in these buffers are lost when the program crashes.

操作系统将缓冲写入,以最大限度地利用有限的磁盘IO。写入通常在五到三十秒内刷新;如果程序员(或程序库)调用 fdatasync(2) fsync(2) sync(2)(它要求所有脏数据被刷新)。如果内核崩溃,则会在程序崩溃时将操作系统缓冲区中的任何数据写入磁盘(最终)。

The OS will buffer writes to make best use of the limited amount of disk IO available. Writes will typically be flushed within five to thirty seconds; sooner if the programmer (or libraries) calls fdatasync(2) or fsync(2) or sync(2) (which asks for all dirty data to be flushed). Any data in the OS buffers are written to disk (eventually) when the program crashes, lost if the kernel crashes.

硬盘驱动器将缓冲区写入,以尽量利用其缓慢的头,旋转延迟等。当操作系统刷新其缓存时,数据到达此缓冲区。当程序崩溃时,这些缓冲区中的数据被写入磁盘,如果内核崩溃,则可能被写入磁盘,并且如果突然移除电源,则可能从驱动器。 (有些人有足够的权力继续写他们的缓冲区,通常这将需要不到一秒钟。)

The hard drive will buffer writes to try to make the best use of its slow head, rotational latency, etc. Data arrives in this buffer when the OS flushes its caches. Data in these buffers are written to disk when the program crashes, will probably be written to disk if the kernel crashes, and might be written to disk if the power is suddenly removed from the drive. (Some have enough power to continue writing their buffers, typically this would take less than a second anyway.)

这篇关于当应用程序崩溃时,如何行为linux缓冲区缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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