QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2262|回复: 3

关于实时监控文件系统(内核中dnotify机制)的问题

[复制链接]
发表于 2006-1-5 12:19:06 | 显示全部楼层 |阅读模式
最近看到网上一篇《linux文件系统桌面应用》的文章,其中提到了系统dnotify机制的实现,代码如下:
#define _GNU_SOURCE /* needed to get the defines */
#include /* in glibc 2.2 this has the needed
values defined */
#include
#include
#include

static volatile int event_fd;

// 信号处理例程
static void handler(int sig, siginfo_t *si, void *data)
{
  event_fd = si->si_fd;
}

int main(void)
{
  struct sigaction act;
  int fd;

  // 登记信号处理例程
  act.sa_sigaction = handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = SA_SIGINFO;
  sigaction(SIGRTMIN, &act, NULL);

  // 需要了解当前目录"."的情况
  fd = open("/root/test1", O_RDONLY);
  fcntl(fd, F_SETSIG, SIGRTMIN);
  fcntl(fd, F_NOTIFY, DN_MODIFY|DN_CREATE|DN_MULTISHOT);
  /* we will now be notified if any of the files
  in "." is modified or new files are created */
  while (1) {
  // 收到信号后,就会执行信号处理例程。
  // 而 pause() 也就结束了。
  pause();
  printf("Got event on fd=%d\n", event_fd);
  }
}

我用KDE编译运行以后,不论在/root/test1中修改文件还是添加目录,console都没有输出,请问是什么原因呢?
发表于 2006-1-5 13:13:25 | 显示全部楼层
dnotify 是要 编译进内核的.
回复

使用道具 举报

 楼主| 发表于 2006-1-5 13:52:05 | 显示全部楼层
恩,谢谢楼上,可是具体请问该怎么做呢?我现在要写一个能够实时监控文件打开和关闭的后台进程,应该是不用重新编译内核的吧
回复

使用道具 举报

发表于 2006-1-5 20:11:37 | 显示全部楼层
我不知道具体怎么做,只知道 要用dnotify/inotify 就必须把它编译进内核或编译成内核模块.

要么你就换一种方法实时监控文件打开和关闭.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-28 02:06 , Processed in 0.060027 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表