C++ iNotify单读
如何使iNotify只读取一次文件 我有以下代码:C++ iNotify单读,c++,inotify,C++,Inotify,如何使iNotify只读取一次文件 我有以下代码: int settingsCheck(int &length, int &i, char * buffer, int &fd, string setRead[]) { int f_change = 0; struct pollfd pfd = { fd, POLLIN, 0 }; /* Poll with a timeout of 100ms */ int ret = poll(&
int settingsCheck(int &length, int &i, char * buffer, int &fd, string setRead[])
{
int f_change = 0;
struct pollfd pfd = { fd, POLLIN, 0 };
/* Poll with a timeout of 100ms */
int ret = poll(&pfd, 1, 100);
/* Check to see the result of the poll */
if (ret < 0) {
fprintf(stderr, "Poll failed: %s\n", strerror(errno));
}
else if (ret == 0) {
/* Timeout with no events -> move on */
}
else {
/* Process the new event */
struct inotify_event event;
length = read(fd, buffer, BUF_LEN);
printf("read\n");
while (i < length)
{
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len)
{
if (event->mask & IN_MODIFY)
{
printf("The file %s was modified.\n", event->name);
readSettings(setRead);
//f_change = 1;
}
}
i += EVENT_SIZE + event->len;
}
//readSettings(setRead);
return 1;
}
return 0;
}
int-settingsCheck(int&length、int&i、char*buffer、int&fd、string-setRead[])
{
int f_change=0;
结构pollfd pfd={fd,POLLIN,0};
/*轮询超时为100毫秒*/
int ret=民意测验(&pfd,1100);
/*查看投票结果*/
如果(ret<0){
fprintf(stderr,“轮询失败:%s\n”,strerror(errno));
}
else if(ret==0){
/*无事件超时->继续*/
}
否则{
/*处理新事件*/
结构inotify_事件;
长度=读取(fd、缓冲区、BUF_LEN);
printf(“读取\n”);
while(i镜头)
{
如果(事件->屏蔽和修改)
{
printf(“文件%s已修改。\n”,事件->名称);
读取设置(设置读取);
//f_变化=1;
}
}
i+=事件大小+事件->镜头;
}
//读取设置(设置读取);
返回1;
}
返回0;
}
这使用poll来确保函数不会被read阻塞。修改文件时,“读取\n”将打印两次,文件将读取两次(我猜第一次读取用于清除文件,第二次读取用于写入文件)。我确保访问该文件的php代码只执行一条“fprintf”语句,因此只有一次写入,而iNotify检查modify读取两次,可能是因为clear和then write
是否有办法解决此问题???每次读取的事件类型是什么?在inotify_add_watch()中注册的标志是什么?