Chinaunix首页 | 论坛 | 博客
  • 博客访问: 988872
  • 博文数量: 96
  • 博客积分: 1553
  • 博客等级: 上尉
  • 技术积分: 1871
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 14:50
个人简介

专注点,细心点,耐心点 知行合一

文章分类

全部博文(96)

文章存档

2018年(1)

2014年(4)

2013年(31)

2012年(56)

2011年(4)

分类: C/C++

2012-02-20 14:00:10


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/event.h>
  4. #include <sys/time.h>

  5. int main()
  6. {
  7.   int kq;
  8.   int n;
  9.   struct kevent event[1];
  10.   struct timespec ts;
  11.   FILE *filep;

  12.   filep = fopen("/home/huxw/test/michael", "r");
  13.   if (!filep) exit(1);

  14.   ts.tv_sec = 0; //设置ts
  15.   ts.tv_nsec = 0;
  16.  
  17.   // 做一个event.
  18.   EV_SET( &event[0], fileno(filep), EVFILT_VNODE,
  19.           EV_ADD,
  20.           NOTE_DELETE | NOTE_ATTRIB | NOTE_WRITE, 0, 0);

  21.   //产生一个queue
  22.   if ( (kq=kqueue()) < 0) exit(2);
  23.   
  24.   //在queue中注册event
  25.   if ( (n = kevent(kq, event, 1, NULL, 0, &ts) < 0) exit(3);

  26.   //ts为NULL, 相当于wait吧, 要poll的话可以还用之前的ts
  27.   n = kevent(kq, NULL, 0, event, 1, NULL);
  28.   if ( n == 0 ) {
  29.      printf("Nothing to do with it\n");
  30.   } else {
  31.     if (event->filter == EVFILT_VNODE && event->fflags == NOTE_DELETE) {
  32.        printf("deleting...\n");
  33.     }
  34.     else if (event->filter == EVFILT_VNODE && event->fflags == NOTE_ATTRIB) {
  35.        printf("attribution changed..\n");
  36.     }
  37.     else if (event->filter == EVFILT_VNODE && event->fflags == NOTE_WRITE) {
  38.        printf("write to it..\n");
  39.     }
  40.   }
  41.   return 0;
  42. }

阅读(2274) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~