Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ inotify描述符上的读取被永久阻止_C++_C_Inotify_File Monitoring - Fatal编程技术网

C++ inotify描述符上的读取被永久阻止

C++ inotify描述符上的读取被永久阻止,c++,c,inotify,file-monitoring,C++,C,Inotify,File Monitoring,我的程序用于监视文件的更改。 我的代码如下: fd = inotify_init(); wd = inotify_add_watch(fd, "./test.txt", IN_ALL_EVENTS); len = read(fd, buff, BUFF_SIZE); while (i < len) { struct inotify_event *pevent = (struct inotify_event *) &buff[i];

我的程序用于监视文件的更改。 我的代码如下:

    fd = inotify_init();
    wd = inotify_add_watch(fd, "./test.txt", IN_ALL_EVENTS);
    len = read(fd, buff, BUFF_SIZE);
    while (i < len) {
        struct inotify_event *pevent = (struct inotify_event *) &buff[i];
                //process events
        i += sizeof(struct inotify_event) + pevent->len;
    }
fd=inotify_init();
wd=inotify\u add\u watch(fd,“./test.txt”,在所有事件中);
len=读取(fd、buff、buff_大小);
而(我len;
}
但是,在我为受监控文件的第一次更改获取了多个事件后(在_OPEN中的事件
、在_MODIFY
中的事件
、在_access中的事件
等),受监控文件的后续更改确实会生成任何其他事件==>我的程序在读取功能处挂起(读取被阻止)


你们能帮我解释一下这个错误吗。

请说明“/test.txt”的实际操作以及关于代码的更多信息(buff,buff\u大小)。下面是一些要点供您检查:

  • 有一些特殊事件,如IN_IGNORED,可能会从文件中删除watch
  • 我建议使用select/poll/epoll来监视fd,而不是阻止对其进行读取

谢谢您的回答。这对我帮助很大。我只想问你另一个问题。为什么当我使用Vim或Gedit打开文件时,我总是在_DELETE_本身中获取事件?@khanhh89我相信您已经编辑了“test.txt”并保存了它。我的vim是7.3,我发现它实际上使用了
open(“test.txt”,O|WRONLY | O|u CREAT | O|u TRUNC,0664)
,并向其写入内容。所以我相信IN_DELETE_本身是由O_TRUNC引起的。@wgwang所以这就是为什么在我读了第一个事件之后,再次使用inotify add_watch(“test.txt”)会导致错误“没有这样的文件或目录”?在
test.txt
上实际发生了什么操作?我尝试在vim打开的情况下进行
inotify
,保存,但在_DELETE_本身
或_IGNORED
中没有
,我尝试了另一个版本:vim 7.0,是的,我得到了你的结果。实际上,当您编辑
test.txt
时,vim 7.0将首先
重命名(“test.txt”、“test.txt~”)
,然后将编辑内容写入
test.txt
,最后
取消链接(“test.txt~”)
。因此,您可以看到原始的
test.txt
实际上已被删除,并且inotify会在删除本身或忽略中获取
事件。对于vim 7.3,这不会发生。