2

我的程序使用以下方式监视文件的更改inotify(7)

fd = inotify_init();
inotify_add_watch (fd, "./test.txt", IN_ALL_EVENTS);
//start forever monitor
while(true){
       ssize_t len, i = 0;
       char action[81+FILENAME_MAX] = {0};
       char buff[BUFF_SIZE] = {0};
       len = read (fd, buff, BUFF_SIZE);
       while (i < len) {
           //process event
           i += sizeof(struct inotify_event) + pevent->len;
       }
}

问题是读取函数只返回几次监视文件中的第一次更改(访问、打开、修改事件)。不幸的是,在那之后,尽管被监控的文件发生了很多变化,但 read 函数不再返回。

但是,如果我重置 inotify 描述符并再次读取监控文件,==> 读取函数总是返回。

//start forever monitor
while(true){
       ssize_t len, i = 0;
       char action[81+FILENAME_MAX] = {0};
       char buff[BUFF_SIZE] = {0};
       fd = inotify_init();
       inotify_add_watch (fd, "./test.txt", IN_ALL_EVENTS);
       len = read (fd, buff, BUFF_SIZE);
       while (i < len) {
           //process event
           i += sizeof(struct inotify_event) + pevent->len;
       }
}

你们能帮我解释一下这个错误吗?非常感谢!!!!!!

4

0 回答 0