This is well answered in this StackOverflow questionin this StackOverflow question.
keep the track of the file offset before the
read()after the
read(), callfadvise(POSIX_FADV_DONTNEED)on the range of data [...]for best results, you have to read the data in page-aligned blocks. The I/O cache is page based, and the
fadvise()maps the specified data range into the list of pages. The misalignment would cause extraread()s (and harm performance) but otherwise is harmless.
You don't want to read ahead if you optimize for memory. You just want to read. A disk will operate sequentially if you read, without any need to tell it to pretty please stream the data.