select+inotify监控linux文件变化遇到的坑
2017-10-20 10:50
459 查看
使用select+inotify监控目录里面文件的创建与删除,发现bug.bug描述如下红色部分:
void listenDev(void * ptr) {
inotifyFd=-1;
inotifyFd=inotify_init();
if(inotifyFd==-1)
return ;
int wd=inotify_add_watch(inotifyFd,"/home/xxx/inotify",IN_DELETE);
printf("WD=%d",wd);
if(wd==-1)
{
close(inotifyFd);
return ;
}
fd_set readFds;
FD_ZERO(&readFds);
FD_SET(inotifyFd, &readFds);
struct timeval val;
val.tv_sec = 2;
val.tv_usec = 0;
int r = -1;
while (flag_is_capturing) {
//bug 描述 当select函数最后一个参数设为NULL时,阻塞的情况下,能正常监控。但是当最后一个参数设为&val时,发现这个超时参数根本无效,将一直疯狂打印timeout,并且监控不到文件夹内文件的删除。 解决方案:将select换成epoll工作正常。 求解答
r = select(inotifyFd+ 1, &readFds, NULL, NULL,
NULL);
if (r == -1) {
flag_is_capturing = 0;
break;
} else if (r == 0) {
printf("timeout \n");
continue;
} else {
printf("dir change \n");
flag_is_capturing=0;
break;
}
}
close(inotifyFd);
}
void listenDev(void * ptr) {
inotifyFd=-1;
inotifyFd=inotify_init();
if(inotifyFd==-1)
return ;
int wd=inotify_add_watch(inotifyFd,"/home/xxx/inotify",IN_DELETE);
printf("WD=%d",wd);
if(wd==-1)
{
close(inotifyFd);
return ;
}
fd_set readFds;
FD_ZERO(&readFds);
FD_SET(inotifyFd, &readFds);
struct timeval val;
val.tv_sec = 2;
val.tv_usec = 0;
int r = -1;
while (flag_is_capturing) {
//bug 描述 当select函数最后一个参数设为NULL时,阻塞的情况下,能正常监控。但是当最后一个参数设为&val时,发现这个超时参数根本无效,将一直疯狂打印timeout,并且监控不到文件夹内文件的删除。 解决方案:将select换成epoll工作正常。 求解答
r = select(inotifyFd+ 1, &readFds, NULL, NULL,
NULL);
if (r == -1) {
flag_is_capturing = 0;
break;
} else if (r == 0) {
printf("timeout \n");
continue;
} else {
printf("dir change \n");
flag_is_capturing=0;
break;
}
}
close(inotifyFd);
}
相关文章推荐
- 通过inotify监控linux文件系统变化
- linux下使用inotify监控文件变化
- 通过inotify监控linux文件系统变化
- 通过inotify监控linux文件系统变化
- linux开发 -- 文件监控 inotify
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 一个监控LINUX目录和文件变化的Shell脚本分享
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 使用inotify实现linux系统下的目录变化监控
- Linux下,借助inotifywait工具,对文件目录进行监控
- Linux操作系统文件变化监控
- [rsync+inotify]——监控客户端文件变化,rsync同步到服务器
- inotifywait命令_Linux inotifywait 命令用法详解:异步文件系统监控机制
- Linux下使用inotify监控文件动作
- linux之tail,监控文件内容变化
- Linux下监测目录或文件的变化---inotify
- linux检测文件和目录变化的api------inotify
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 使用inotify监视Linux文件变化