How does linux cache buffer work when an application crashes?

Let's say I use an asynchronous C ++ file stream. I mean never use std :: flush or std :: endl. My application writes a large amount of data to a file and drops sharply. Are the data remaining in the cache system saved to disk or discarded (and lost)?

+2
source share
2 answers

The complication of this problem is that there are several "caches" in the game.

C ++ streams have their own internal buffering mechanism. Streams do not ask the OS to write to disk until (a) you send a sufficient amount of data to a buffer, which the stream library considers that the record will not be wasted. (B) you request a flash specifically (c) the stream is in line buffering mode and you sent it via endl . Any data in these buffers is lost when the program crashes.

The OS will buffer recordings to make the best use of the limited number of available IO disks. Records will usually be discarded within five to thirty seconds; if the programmer (or library) calls fdatasync(2) or fsync(2) or sync(2) (which requests to clear all dirty data). Any data in the OS buffers is written to disk (in the end), when the program crashes, it is lost if the kernel crashes.

The hard disk will buffer recordings to try best to use slow head, rotational delay, etc. Data arrives in this buffer when the OS flushes its caches. Data in these buffers is written to disk when the program crashes, they will probably be written to disk if the kernel crashes and can be written to disk if the power is suddenly removed from the disk. (Some have enough power to keep writing their buffers, usually it takes less than a second.)

+10
source

Material in the library buffer (which you flush with std :: flush or something) is lost, data in the OS kernel buffers (which you can flush with fsync (), for example) are not lost if the OS itself does not work.

0
source

All Articles