The read method on the inotify descriptor does not

2019-08-02 03:43发布

问题:

My program monitors changes on files using 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;
       }
}

The problem is that the read function only return several times for the firt changes in the monitor file (ACCESS, OPEN, MODIFY events). Unfortunately after that, the read function does not return any more although there a lot of changes in the monitored file.

However, If I reset the inotify descriptor and readd the monitered file agains, ==> the read function always return.

//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;
       }
}

Could you guys help me explain this error. Thanks so much!!!!!!