c++ciofcntl

How is a read system call different from the istream::read function?


My Operating Systems professor was talking today about how a read system call is unbuffered while a istream::read function has a buffer. This left me a bit confused as you still make a buffer for the istream::read function when using it.

The only thing I can think of is that there are more than one buffers in the istream::read function call. Why?

What does the istream::read() function do differently from the read() function system call?


Solution

  • The professor was talking about buffers internal to the istream rather than the buffer provided by the calling code where the data ends up after the read.

    As an example, say you are reading individual int objects out of an istream, the istream is likely to have an internal buffer where some number of bytes is stored and the next read can be satisfied out of that rather than going to the OS. Note, however, that whatever the istream is hooked to very likely has internal buffers as well. Most OSes have means to perform zero-copy reads (that is, read directly from the I/O source to your buffer), but that facility comes with severe restrictions (read size must be multiple of some particular number of bytes, and if reading from a disk file the file pointer must also be on a multiple of that byte count). Most of the time such zero-copy reads are not worth the hassle.