title: Why does the following prints 'Resource temporarily unavailable'?
body: <p>Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time? That is the EAGAIN code, which is the same as WOULD BLOCK which means there is no data waiting to be read, but select is returning 1 saying there is data (tested in Linux):</p>
  
  <pre><code>#include &lt;time.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/errno.h&gt;
  
  int main(int argc, char** argv)
  {
      int fd = open("/dev/lp0", O_RDWR | O_NONBLOCK);
      int ret = 0;
      int status = 0;
      char buffer[1024];
      char teststr[] = "This is a test\n";
      char XMIT_STATUS_OFFLINE[] = {0x10,0x04,0x02};
      char XMIT_STATUS_ERROR[] = {0x10,0x04,0x03};
      char XMIT_STATUS_ROLL[] = {0x10,0x04,0x04};
      char XMIT_STATUS_SLIP[] = {0x10,0x04,0x05};
      fd_set rfds;
      FD_ZERO( &amp;rfds );
      FD_SET( fd, &amp;rfds );
      struct timeval sleep;
      sleep.tv_sec = 5;
      sleep.tv_usec = 0;
  
      /* Offline status */
      ret = write(fd, XMIT_STATUS_OFFLINE, sizeof(XMIT_STATUS_OFFLINE));
      //printf("write() returned %d\n", ret);
  
      do {
          ret = select( fd + 1, &amp;rfds, NULL, NULL, &amp;sleep );
      } while (ret &lt; 0 &amp;&amp; (errno == EINTR));
  
      ret = read(fd, buffer, 1024);
      if(ret == -1) {
          perror("read(): ");
      } else {
          status = buffer[0];
          if((status &amp; 0x04) != 0)
          {
              printf("The cover is open.\n");
          } else {
              printf("OFFLINE is good.\n");
          }
      }
      close(fd);
      return 0;
  }
  </code></pre>