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

查看:21
本文介绍了当应用程序崩溃时,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)?

推荐答案

使这个问题复杂化的是有多个缓存"在起作用.

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

C++ 流有自己的内部缓冲机制.流不会要求操作系统写入磁盘,直到 (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天全站免登陆