inotify API 与 select

问题描述 投票:0回答:1

我正在尝试将 inotify 与 select() 调用一起使用。

time_out 设置为 3 秒。我似乎无法让它工作。

选择始终返回零。

代码片段,

    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");
            ......
    }

如果我不使用 select,它工作得很好。它也可以与 poll() 配合使用。

我还尝试了 inotify_init1() 和 IN_NONBLOCK

有人能告诉我我做错了什么吗?

c linux inotify posix-select
1个回答
4
投票

首先,

select
的第一个参数是任何集合中的最高描述符加一。所以在你的情况下应该是
fd + 1

其次,

select
函数修改集合,因此您需要在循环中的每次迭代中添加描述符。

© www.soinside.com 2019 - 2024. All rights reserved.