cdsfiui 发表于 2016-12-27 09:18

fcntl(F_NOTIFY监视文件和目录,但没有触发任何响应?

本帖最后由 cdsfiui 于 2016-12-28 09:28 编辑

我在用 RHEL5 + kernel 2.6.32. 常使用 fcntl+F_NOTIFY来监视一个目录和一个文件的变化. 我从IBM developer networks里面找到一个例子,尝试编译运行:
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static volatile int event_fd;

static void handler(int signum, siginfo_t *si, void *data){
event_fd = si->si_fd;
printf("info size:%d, data:%d\n", sizeof(siginfo_t), sizeof(data));
}

int main(int argc, char **argv){
struct sigaction action;
int fd;
action.sa_sigaction = handler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO;
sigaction(SIGRTMIN+1, &action, NULL);

fd = open("./test.txt", O_RDONLY);
fcntl(fd, F_SETSIG, SIGRTMIN+1);
fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_MULTISHOT);

fd = open(".", O_RDONLY);
fcntl(fd, F_SETSIG, SIGRTMIN+1);
fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_MULTISHOT);
while(1){
    pause();
    printf("got event on fd=%d\n", event_fd);
}
}
首先我touch test.txt,然后运行a.out
在另一个窗口里面echo "content">>test.txt 改变其内容。

我期待的是,handler函数会被运行,因为信号来了。但是实际上什么也没有发生。
我在当前目录里面创建删除其他的文件,期待会触发信号,但是实际什么也没有发生。

这是为何,我的程序有问题还是理解有误。

hellioncu 发表于 2016-12-27 10:43

试试signal(SIGIO, your_handle)
页: [1]
查看完整版本: fcntl(F_NOTIFY监视文件和目录,但没有触发任何响应?