- 论坛徽章:
- 0
|
程序是这样的(监视两个日志文件的改变):
int fd = inotify_init();
fd_set rfds;
FD_ZERO(&rfds);
int ld1 = inotify_add_watch(fd, file_name1, IN_MODIFY);
FD_SET(ld1, &rfds);
int ld2 = inotify_add_watch(fd, file_name2, IN_MODIFY);
FD_SET(ld2, &rfds);
int nfds = 2;
while (true) {
if (nfds == 0) {
LOG_ERROR("No log notify fd need be monitored.");
break;
}
int retval = select(nfds, &rfds, NULL, NULL, NULL);
if (retval == -1) {
PLOG_ERROR("select for log file");
return EXIT_FAILURE;
}
int rfd = FD_ISSET(ld1, &rfds) ? ld1 : ld2;
char buffer[EVTBUF_LEN] = {0};
int length = read(rfd, buffer, EVTBUF_LEN);
.......
}
但是执行过程中发现日志文件虽然在改变,但是select函数一直阻塞,不返回。如果不使用select,直接read两个inotify watch,是能够读取到文件改变事件的。
请问下这是怎么回事?是我的用法不对吗? |
|