0

I'm trying to accurately determine some of the interactions between a userland program using the C/C++ library and the OS (and its components like the filesystem's Page Cache or Disk Cache). I have two questions.

First, is "unbuffered" in C/C++ library equivalent to "no read ahead" and "no caching" in the OS. Or am I suffering a disconnect?

Second, is it possible to perform un-cached file operations and unbuffered reads using C++'s streams? If so, how does one do it since there does not seem to be any open flags related to it?


The reason I ask is similar to the following. I'm working with a C++ library that provides integration with C++ streams, and I want to make sure I'm not getting any unintentional side effects.

ifstream("/dev/random");
...

If the stream triggers a read of block or sector size (like 4K or 8K) but I only need 32 bytes, then I'm wasting a lot of potential entropy. I might even deplete it and DoS myself and other programs.

I feel like this question has probably been asked and answered, but I'm not having much luck in finding it.


Here's a related question (thanks to Dietmar): How to disable buffering on a stream?. But it does not address or discuss the unintended side effects that may be present due to OS or filesystem read ahead and caching.

4

1 回答 1

4

在文件流上,您可以将流设置为不使用缓冲区

std::ifstream in;
in.rdbuf()->pubsetbuf(0, 0);
in.open("/dev/random");
于 2015-02-27T05:02:05.143 回答