title: Can read(2) return zero when not at EOF?
body: <p>According to the man page for read(2), it only returns zero when EOF is reached.</p>
  
  <p>However, It appears this is incorrect and that it may sometimes return zero, perhaps because the file is not ready to be read yet?  Should I call select() to see if it is ready before reading a file from disk?</p>
  
  <p>Note that nBytes is: 1,445,888</p>
  
  <p>Some sample code:</p>
  
  <pre><code>fd_set readFdSet;
  timeval timeOutTv;
  
  timeOutTv.tv_sec = 0;
  timeOutTv.tv_usec = 0;
  
  // Let's see if we'll block on the read.
  FD_ZERO(&amp;readFdSet);
  FD_SET(fd, &amp;readFdSet);
  
  int selectReturn = ::select(fd + 1, &amp;readFdSet, NULL, NULL, &amp;timeOutTv);
  
  if (selectReturn == 0) {
    // There is still more to read.
    return false; // But return early.
  } else if (selectReturn &lt; 0) {
    clog &lt;&lt; "Error: select failure: " &lt;&lt; strerror(errno) &lt;&lt; endl;
    abort();
  } else {
    assert(FD_ISSET(fd, &amp;readFdSet));
  
    try {
      const int bufferSizeAvailable = _bufferSize - _availableIn;
  
      if (_availableIn) {
        assert(_availableIn &lt;= _bufferSize);
  
        memmove(_buffer, _buffer + bufferSizeAvailable, _availableIn);
      }
  
      ssize_t got = ::read(fd, _buffer + _availableIn, bufferSizeAvailable);
  
      clog &lt;&lt; " available: " &lt;&lt; bufferSizeAvailable &lt;&lt; " availableIn: "
           &lt;&lt; _availableIn &lt;&lt; " bufferSize: " &lt;&lt; _bufferSize &lt;&lt; " got "
           &lt;&lt; got &lt;&lt; endl;
  
      return got == 0;
    } catch (Err &amp;err) {
      err.append("During load from file.");
      throw;
    }
  }
  </code></pre>
  
  <p>The output reads (when it fails with no data read):</p>
  
  <pre><code>available: 1445888 availableIn: 0 bufferSize: 1445888 got: 0
  </code></pre>
  
  <p>This is running on centos4 32 bit as a virtual machine using VMware Server 1.0.10.  The file system being read is local to the virtual machine.  The host machine is windows server 2008 32 bit.</p>
  
  <p>The uname -a says:</p>
  
  <pre><code>Linux q-centos4x32 2.6.9-89.0.25.ELsmp #1 SMP Thu May 6 12:28:03 EDT 2010 i686 i686 i386 GNU/Linux
  </code></pre>
  
  <p>I notice that the link <a href="http://opengroup.org/onlinepubs/007908775/xsh/read.html" rel="noreferrer">http://opengroup.org/onlinepubs/007908775/xsh/read.html</a> given below states:</p>
  
  <pre><code>The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal...
  
  If a read() is interrupted by a signal before it reads any data, it will return -1 with errno set to [EINTR].
  
  If a read() is interrupted by a signal after it has successfully read some data, it will return the number of bytes read. 
  </code></pre>
  
  <p>So, perhaps I am getting a signal interrupting the read and thus the value returned is zero because of either a bug or it thinks zero bytes were read?</p>