Hello
I'm currently writing a software to test the integrity of flash-memory devices. (using c++ iostreams)

To achieve this goal files with random content are written to the device, read back, copied, moved, compared ( I put the device under stress condition, and check if the data is valid from time to time)...
Everything looks great on the outside, but there is a problem behind the curtains.

When I analyzed the commands that are sent to the device over the ATA / SATA Cable (with a Bus Doctor), I discovered that (especially) read commands are often not executed. Nevertheless the data is availible to the program. So they have to come from a buffer, which is not acceptable in my case. Whenever I read Data it has to come straight from the disk, not from the cache. Read / Write Performance isn't important at all.

But I haven't found a way to tell Windows not to buffer anything (There is the option to deactivate write caching for a specific device in the device manager, but doesn't have the desired effect).
If anyone knows how to do this: Please tell me
( same goes for any advice concerning Linux. I think I will run into the same problems there )

Only solution I could come up with so far is Direct I/O.
What I found for Windows is this:

The WinIo library allows 32-bit Windows applications to directly access I/O ports and physical memory. It bypasses Windows protection mechanisms by using a combination of a kernel-mode device driver and several low-level programming techniques.

Direct I/O is the world's first device driver for Microsoft Windows NT, Windows 2000, Windows XP and Windows Server 2003 which enables the direct hardware access for your existing software without any programming efforts on your side

Questions:
Has somebody ever had a similar problem, and can give me some advice?
Do I really have to go with Direct I/O or is there some OS option/setting I haven't found (Windows or Linux)
Do you know other libraries for Direct I/O (Windows or Linux or even better: both)

Why don't you just turn of the caching in both the system and iostream?
–
Let_Me_BeOct 26 '10 at 15:35

@Let_Me_Be: With the options I found (disabling the write cache in the device manager and flusing / disabling the iostream buffers) I couldn't achieve the desired results.
–
zitroneneisOct 26 '10 at 17:47

2 Answers
2

When opening or creating a file with
the CreateFile function, the
FILE_FLAG_NO_BUFFERING flag can be
specified to disable system caching of
data being read from or written to the
file. Although this gives complete and
direct control over data I/O
buffering, in the case of files and
similar devices there are data
alignment requirements that must be
considered.

Not sure if this can be done in the C++ standard library. You can disable buffering at the iostream level but that may not affect the O/S buffering.

Thank you for the link and the advice! Concerning the buffering at the iostream level: I know that it can be disabled but I already flush the buffers after every read/write access to the device. (I also tried to turn them off completely but it didn't help)
–
zitroneneisOct 26 '10 at 17:41