clinuxinotifyposix-select

inotify API with select


I am trying to use inotify with a select() call.

The time_out is set at 3 secs. I cannot seem to get it to work.

The select always returns zero.

Code snippet,

    fd_set fds;

    //fd is descriptor for inotify_init
    FD_ZERO(&fds);
    FD_SET(fd, &fds);

    struct timeval time_wait;
    while(true)
    {
            time_wait.tv_sec=3;
            time_wait.tv_usec=0;
            i = 0;
            res = select(fd , &fds, NULL, NULL, &time_wait);

            if (ret == -1) {
                    printf("Error occured in inotify read \n");
                    break;
            }

            if(ret == 0) {         
                    //always triggers
                    printf("timed out \n");
                    continue;
            }

            //Never reaches here
            printf("Event occured \n");
            ......
    }

If I do not use select, it works fine. And it also works well with poll().

I also tried inotify_init1() with IN_NONBLOCK

Can someone tell what I'm doing wrong?


Solution

  • First of all, the first argument to select is the highest descriptor in any set plus one. So in your case it should be fd + 1.

    Secondly, the select function modifies the sets, so you need to add the descriptors every iteration in the loop.