title: Select() doesnt behave properly with an eventfd
body: <p>I want to use eventfd as a way to signal simple events between kernelspace and userspace. eventfd will be used as a way to signal and the actual data will be transferred using ioctl. 
  Before going ahead with implementing this, I wrote a simple program to see how eventfd behaves with select(). It seems that if you use select to wait on an eventfd, it wont return when u write to it in a separate thread. In the code I wrote, the writing thread waits for 5 seconds beginning from program start before writing to the eventfd twice. I would expect the select() to return in the reading thread immediately following this write but this does not happen. The select() returns only after the timeout of 10 seconds and returns zero. Regardless of this return zero, when I try to read the eventfd after 10 seconds, I get the correct value. </p>
  
  <p>I use Ubuntu 12.04.1 (3.2.0-29-generic-pae) i386</p>
  
  <p>Any idea why this is so? It seems to me that select() is not working as it should.</p>
  
  <p>PS: This question is similar to <a href="https://stackoverflow.com/questions/5355791/linux-cant-get-eventfd-to-work-with-epoll-together">linux - Can&#39;t get eventfd to work with epoll together</a> </p>
  
  <p>Is anyone else facing similar issues?</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;     //Definition of uint64_t
  
  #include &lt;pthread.h&gt;    //One thread writes to fd, other waits on it and then reads it
  #include &lt;time.h&gt;       //Writing thread uses delay before writing
  #include &lt;sys/eventfd.h&gt;
  
  int efd; //Event file descriptor
  
  void * writing_thread_func() {
      uint64_t eftd_ctr = 34;
      ssize_t s;
  
      printf("\n%s: now running...",__func__);
  
      printf("\n%s: now sleeping for 5 seconds...",__func__);
      fflush(stdout); //must call fflush before sleeping to ensure previous printf() is executed
      sleep(5);
  
      printf("\n%s: Writing %lld to eventfd...",__func__,eftd_ctr);
      s = write(efd, &amp;eftd_ctr, sizeof(uint64_t));
      if (s != sizeof(uint64_t)) {
          printf("\n%s: eventfd writing error. Exiting...",__func__);
          exit(EXIT_FAILURE);
      }
  
      eftd_ctr = 99;
  
      printf("\n%s: Writing %lld to eventfd...",__func__,eftd_ctr);
      s = write(efd, &amp;eftd_ctr, sizeof(uint64_t));
      if (s != sizeof(uint64_t)) {
          printf("\n%s: eventfd writing error. Exiting...",__func__);
          exit(EXIT_FAILURE);
      }
  
      printf("\n%s: thread exiting...",__func__);
      pthread_exit(0);
  }
  
  void * reading_thread_func() {
      ssize_t s;
      uint64_t eftd_ctr;
  
      int retval;         //for select()
      fd_set rfds;        //for select()
      struct timeval tv;  //for select()
  
      printf("\n%s: now running...",__func__);
  
      printf("\n%s: now waiting on select()...",__func__);
      //Watch efd
      FD_ZERO(&amp;rfds);
      FD_SET(efd, &amp;rfds);
  
      //Wait up to 10 seconds
      tv.tv_sec = 10;
      tv.tv_usec = 0;
  
      retval = select(1, &amp;rfds, NULL, NULL, &amp;tv);
  
      if (retval == -1){
          printf("\n%s: select() error. Exiting...",__func__);
          exit(EXIT_FAILURE);
      } else if (retval &gt; 0) {
          printf("\n%s: select() says data is available now. Exiting...",__func__);
          printf("\n%s: returned from select(), now executing read()...",__func__);
          s = read(efd, &amp;eftd_ctr, sizeof(uint64_t));
          if (s != sizeof(uint64_t)){
              printf("\n%s: eventfd read error. Exiting...",__func__);
              exit(EXIT_FAILURE);
          }
          printf("\n%s: Returned from read(), value read = %lld",__func__, eftd_ctr);
      } else if (retval == 0) {
          printf("\n%s: select() says that no data was available even after 10 seconds...",__func__);
          printf("\n%s: but lets try reading efd count anyway...",__func__);
          s = read(efd, &amp;eftd_ctr, sizeof(uint64_t));
          if (s != sizeof(uint64_t)){
              printf("\n%s: eventfd read error. Exiting...",__func__);
              exit(EXIT_FAILURE);
          }
          printf("\n%s: Returned from read(), value read = %lld",__func__, eftd_ctr);
          exit(EXIT_FAILURE);
      }
  
      printf("\n%s: thread exiting...",__func__);
      pthread_exit(0);
  }
  
  int main() {
      pthread_t writing_thread_var, reading_thread_var;
  
      //Create eventfd
      efd = eventfd(0,0);
      if (efd == -1){
          printf("\n%s: Unable to create eventfd! Exiting...",__func__);
          exit(EXIT_FAILURE);
      }
  
      printf("\n%s: eventfd created. value = %d. Spawning threads...",__func__,efd);
  
      //Create threads
      pthread_create(&amp;writing_thread_var, NULL, writing_thread_func, NULL);
      pthread_create(&amp;reading_thread_var, NULL, reading_thread_func, NULL);
  
      //Wait for threads to terminate
      pthread_join(writing_thread_var, NULL);
      pthread_join(reading_thread_var, NULL);
  
      printf("\n%s: closing eventfd. Exiting...",__func__);
      close(efd);
      exit(EXIT_SUCCESS);
  }
  </code></pre>