5

I lack formal knowledge in Operating systems and C. My questions are as follows.

  1. When I try to read first single byte of a file using fread in C, does the entire disk block containing that byte is brought into memory or just the byte?
  2. If entire block is brought into memory, what happens on reading second byte since the block containing that byte is already in memory?.
  3. Is there significance in reading the file in size of disk blocks?
  4. Where is the read file block kept in memory?

1 Answers1

5

Here's my answers

  1. More than 1 block, default caching is 64k. setvbuffer can change that.
  2. On the second read, there's no I/O. The data is read from the disk cache.
  3. No, a file is ussuly smaller than it's disk space. You'll get an error reading past the file size even if you're within the actual disk space size.
  4. It's part of the FILE structure. This is implementation (compiler) specific so don't touch it.

The above caching is used by the C runtime library not the OS. The OS may or may not have disk caching and is a separate mechanism.

egur
  • 7,830
  • 2
  • 27
  • 47
  • The caching for the OS is a different memory then the buffer used in the user space `FILE` structure. The `FILE` buffer is set on an indvidual basis, only the default is set the by the compilaer environment. – Devolus Dec 07 '13 at 16:02
  • 1
    Correct, I've added a short explanation to the answer. Also fixed typos :( – egur Dec 07 '13 at 16:04