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.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

4

On file streams you can set the stream to not use a buffer using

std::ifstream in;
in.rdbuf()->pubsetbuf(0, 0);
in.open("/dev/random");
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Thanks @Dietmar. Does that disable read ahead and caching? Or will there be unintended side effects? – jww Feb 27 '15 at 05:05
  • Forgive my ignorance... Will it disable read ahead and caching performed by the OS? Here, I'm making the leap that C and C++ libraries "buffer", while lower level components like file systems "read ahead" and "cache". This may be wrong, so I'm trying to get a clarification on that, too :) – jww Feb 27 '15 at 05:23
  • Do you happen to know if unbuffered IO is faster than buffered IO when dealing with shared memory as well or is it just regular files/memory? – Brandon Feb 27 '15 at 06:34
  • This answers the question of **buffered I/O**. Waiting for the answer to **bypass page cache**. – A. Gille Apr 22 '21 at 14:37