previous | start | next

Buffering

The C standard i/o functions create buffers in your linked program so that if you write a program that reads a file 1 character at a time, it doesn't ask the Linux file system for 1 character, but rather it fills an internal buffer.

Then each request for a single character just retrieve the next character from the buffer in your code until the buffer is empty.

When the buffer is empty, another request is made behind the scenes to Linux I/O to fill the buffer.

The csapp.c similarly creates buffers (a struct with a character array) for the read and provides buffered version of rio_readn, with no short reads except for the next to last read and the last EOF read.

      rio_t rb; // buffer struct
      int fd;   // for file descriptor
      ...
      const int N = 8;
      char buf[N];
      int nrd;

      rio_readinitb(&rb, fd);
      ...
      nrd = rio_read(&rb, buf, N);  
   


previous | start | next