0

I am using select() function to listen the events from all file descriptors including stdin. The select function is always called in time but no matter whatever event happened and whatever fd triggers the event ,the select() function always returns 1.

    void startSelecting()
    {
       printf("ready! start listening all events\n");
      int current;
      while(is_running){

         do{
             current=select(1000,&readset,NULL,NULL,NULL);
           }while(current==-1&&errno==EINTR);
              printf("file descriptor %d has event\n",current);
              processEvents(current);

        }


    }

So,that is,it always prints "file descriptor 1 has event"... And,I am implementing C language Code. thank you guys

user3788871
  • 25
  • 1
  • 4
  • 10
  • 1
    There's almost certainly a duplicate of this question. The problem I see with your code is that the `select()` clobbers the `readset` and you don't restore it, which means you ask a different question on the second iteration. The other possible issue is that if you have any disk files in the file descriptor list, they're always readable. Note that `select()` returns the number of readable descriptors, not the number of the (only) readable descriptor. You will need to show how `readset` is constructed to get much more help. – Jonathan Leffler Sep 21 '14 at 20:50
  • See [Why `select()` always returns 0 after the first timeout?](http://stackoverflow.com/questions/3324078/why-select-always-return-0-after-the-first-timeout/3324123#3324123) and [Are there any platforms where using structure copy on an `fd_set` (for `select()` or `pselect()`) causes problems?](http://stackoverflow.com/questions/2421672). Also [How to make `select()` block on a file descriptor?](http://stackoverflow.com/questions/16944727/) and [Is it necessary to reset the `fd_set` between `select()` system calls?](http://stackoverflow.com/questions/4563577) – Jonathan Leffler Sep 21 '14 at 20:54
  • The select function returns the number of file descriptors (the number of bits from readfds, writefds, and errorfds). So at the time of being you only have 1 file descriptor. – Gasim Sep 21 '14 at 21:00

1 Answers1

0

On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets

So if only one file descriptor triggers the event (which is your case), select() returns 1. And after select() returns, your readset therefore contains only one fd - the one which triggered the event.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • Yeah,I read the documents carefully, and I found that I misunderstood what is returned by the function. So,does it mean,every time entering the loop,the fd_set should be zeroed and when some events are trigged by file_descriptors,the select() will return the number of fd and put the fd numbers into the fd_set? But what FD_SET() is for,why do we need to add a file_descriptor in to the fd_set? – user3788871 Sep 21 '14 at 21:08
  • @user3788871: no; you have to use FD_SET() etc to create the list of descriptors you want `select()` to check on each call. When the call returns, the fd set contains the list of descriptors which can be read (in your code) without blocking. So, you have to reset the fd set on each iteration. And you can't use it sensibly with a disk file descriptor. Given this part of the misunderstanding, I've closed the question as a duplicate of one that deals with this issue of resetting the fd set on each iteration — it seems most appropriate. – Jonathan Leffler Sep 21 '14 at 21:11