tags:

views:

109

answers:

3

How can I write something to a file in C++ without using system cache and drive cache? I just want to write exactly on the hdd regardless all the system cache settings.

+1  A: 

On Linux you can pass O_DIRECT to open(2) in order to try to avoid the OS cache, but you don't have the same level of control over the drive cache.

Ignacio Vazquez-Abrams
+2  A: 

This is highly operating system dependent. On Windows you can specify FILE_FLAG_NO_BUFFERING when you open the file with CreateFile to disable system caching. You won't get around hard disk caching though.

villintehaspam
If you pass both `FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING`, Windows will tell the disk to bypass its caching too.
MSalters
+3  A: 

Unless you are writing a disk device driver, you can't guarantee that there won't be any cache or processing done with your write.

The C runtime library exposes fflush(FILE *) to do this. Windows has FlushFileBuffers as well as a flag you can pass to CreateFile (FILE_FLAG_NO_BUFFERING) (which itself adds restrictions on what you can do).

An alternative to trying to bypass write caching is to make your data structures resilient to partial failure. For files, one popular technique is to write out the header of the file after the rest of the file is written. Assuming Murphy and not Machiavellian behavior, that should be enough.

Or use the OS provided file replacement or transaction functions (See ReplaceFile and Transactional NTFS for Windows).

MSN
Actually, that doesn't even protect against Murphy. Common write reordering will cause writes to be reordered in physical order. Even though the header is provided to the OS last, it may hiot disk first if there's enough buffer available. Note that modern disks can do this reordering themselves, outside the OS. (SATA NCQ)
MSalters