title: select() returns invalid argument
body: <p>I am successfully reading from a pipe from another thread, and printing the output (in an ncurses window as it happens).</p>
  
  <p>I need to do this one character at a time, for various reasons, and I'm using a select() on the FD for the read end of the pipe, along with a few other FDs (like stdin).</p>
  
  <p>My idea is to attempt to read from the pipe only when it is imminently ready to be read, in preference to handling any input. This seems to be working - at least to start. select() sets up the fd_set and if FD_ISSET I do my read() of 1 byte from the FD. But select() says yes one time too many, and the read() blocks.</p>
  
  <p>So my question is this - why would select() report that a fd is ready for reading, if a subsequent read() blocks? </p>
  
  <p>(approximately) This same code worked fine when the other end of the pipe was connected to a forked process, if that helps.</p>
  
  <p>I can post the code on request, but it's bog standard. Set up a fd_set, copy it, select the copy, if the FD is set call a function which reads a byte from the same FD... otherwise revert the fd_set copy</p>
  
  <p>EDIT: by request, here's the code:</p>
  
  <p>Setting up my fd_set:</p>
  
  <pre><code>fd_set fds;
  FD_ZERO(&amp;fds); 
  FD_SET(interp_output[0], &amp;fds);
  FD_SET(STDIN_FILENO, &amp;fds);
  struct timeval timeout, tvcopy; timeout.tv_sec=1;
  int maxfd=interp_output[0]+1; //always &gt;stdin+1
  fd_set read_fds;
  FD_COPY(&amp;fds, &amp;read_fds);
  </code></pre>
  
  <p>In a loop:</p>
  
  <pre><code>if (select(maxfd, &amp;read_fds, NULL, NULL, &amp;timeout)==-1) {perror("couldn't select"); return;}
  if (FD_ISSET(interp_output[0], &amp;read_fds)) {
      handle_interp_out();
  } else if (FD_ISSET(STDIN_FILENO, &amp;read_fds)) {
  //waddstr(cmdwin, "stdin!"); wrefresh(cmdwin);
      handle_input();
  }
  
  FDCOPY(&amp;fds, &amp;read_fds);
  </code></pre>
  
  <p>handle_interp_out():</p>
  
  <pre><code>void handle_interp_out() {
      int ch;
      read(interp_output[0], &amp;ch, 1);
      if (ch&gt;0) {
              if (ch=='\n') { if (cmd_curline&gt;=cmdheight) cmdscroll(); wmove(cmdwin, ++cmd_curline, 1); }
              else waddch(cmdwin, ch);
              wrefresh(cmdwin);
      }
  }
  </code></pre>
  
  <p>EDIT 2: The write code is just a fprintf on a FILE* opened with fdopen(interp_output[1], "w") - this is in a different thread. All I'm getting to is my "prompt> " - it prints all that properly, but does one more iteration that it shouldn't. I've turned off buffering, which was giving me other problems.</p>
  
  <p>EDIT 3: This has become a problem with my invocation of select(). It appears that, right away, it's returning -1 and errno's being set to 'invalid argument'. The read() doesn't know that and just keeps going. What could be wrong with my select()? I've updated the code and changed the title to more accurately reflect the problem...</p>
  
  <p>EDIT 4: So now I'm thoroughly confused. The timeout value of .tv_sec=1 was no good, somehow. By getting rid of it, the code works just fine. If anybody has any theories, I'm all ears. I would just leave it at NULL, except this thread needs to periodically do updates.</p>