免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6150 | 回复: 12
打印 上一主题 下一主题

[C] 监控多个文件目录 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-28 10:50 |只看该作者 |倒序浏览
我现在要开发一个程序,程序的功能是监控30个目录。一旦该目录下有新文件就对新文件作处理,但这30个目录产生新文件的时间都不统一。我想用多线程的方式去监控处理这些目录,一个线程监控一个,如果这个目录下没有文件,该线程就休眠。但领导的意思是用单线程的方式来监控,这样会在维护上比较好控制。

大家看看有哪种办法更可靠方便,或者有啥更好的想法呢

论坛徽章:
0
2 [报告]
发表于 2009-12-28 11:06 |只看该作者
inotify

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2009-12-28 11:09 |只看该作者
你需要考虑四个问题:
1,启动的时候,对启动前已经存在的目录和文件如何处理;
2,运行的过程中,如果目录被删除了如何处理。
3,对性能的要求
4,对实时性的要求


可以用 inotify(7) 做,但是要考虑必要性,因为这会让程序变得复杂。

论坛徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
4 [报告]
发表于 2009-12-28 11:12 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
5 [报告]
发表于 2009-12-28 11:20 |只看该作者
如果采用轮询的方式,一个线程监控30个目录跟一个线程监控1个目录有多大的区别?

论坛徽章:
0
6 [报告]
发表于 2009-12-28 12:40 |只看该作者
FAM
lighttpd里用的这个来监控web资源文件是否被修改,lz可以参考一下。

http://oss.sgi.com/projects/fam/index.html
http://www.gnome.org/~veillard/gamin/index.html

论坛徽章:
0
7 [报告]
发表于 2009-12-28 15:02 |只看该作者
还是inotify吧

论坛徽章:
0
8 [报告]
发表于 2009-12-28 15:31 |只看该作者
tripwire

论坛徽章:
0
9 [报告]
发表于 2009-12-29 12:07 |只看该作者

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/select.h>
  4. #include <errno.h>
  5. #include <sys/inotify.h>

  6. typedef struct wd_name {
  7.         int wd;
  8.         char * name;
  9. }WD;

  10. #define MONITOR_NUM  3
  11. char* files[MONITOR_NUM] = {
  12.         "./a",
  13.         "./b",
  14.         "./c"
  15. };

  16. WD wd_array[MONITOR_NUM];

  17. static void inotify_event_handler(struct inotify_event *event)
  18. {  
  19.         int i = 0;
  20.         if (event->mask & IN_ISDIR)
  21.          printf("Object type: %s\n", "Direcotory");
  22.         else
  23.         printf("Object type: %s\n", "file");
  24.        
  25.         for (i=0; i<MONITOR_NUM; i++)
  26.          {
  27.                 if (event->wd != wd_array[i].wd)
  28.                         continue;
  29.                 printf("Object name: %s\n", wd_array[i].name);
  30.                 break;
  31.          }
  32.        
  33.          if ( event->mask & IN_CREATE )
  34.                  {
  35.      if ( event->mask & IN_ISDIR )
  36.       printf( "The directory %s was created.\n", event->name );      
  37.      else
  38.       printf( "The file %s was created.\n", event->name );
  39.     }
  40.    else if ( event->mask & IN_DELETE )
  41.     {
  42.      if ( event->mask & IN_ISDIR )
  43.        printf( "The directory %s was deleted.\n", event->name );      
  44.      else
  45.        printf( "The file %s was deleted.\n", event->name );
  46.     }
  47.    else if ( event->mask & IN_MODIFY )
  48.            {
  49.      if ( event->mask & IN_ISDIR )
  50.        printf( "The directory %s was modified.\n", event->name );
  51.      else
  52.        printf( "The file %s was modified.\n", event->name );
  53.     }
  54. }

  55. int main(int argc, char **argv)
  56. {  
  57. int fd = 0;
  58. int wd = 0;
  59. int i = 0;
  60. int len = 0;
  61. int index = 0;  
  62.        
  63. unsigned char buf[1024] = {0};  
  64. struct inotify_event *event = {0};  

  65. fd = inotify_init();
  66. if (fd < 0) {
  67.                 printf("Fail to initialize inotify.\n");
  68.                 return -1;
  69.         }
  70.        
  71. for (i=0; i<MONITOR_NUM; i++) {
  72.                 wd_array[i].name = files[i];
  73.                 wd = inotify_add_watch(fd, wd_array[i].name, IN_CREATE | IN_MODIFY | IN_DELETE);
  74.                 if (wd < 0) {
  75.                         printf("Can't add watch for %s.\n", wd_array[i].name);
  76.                         return -1;
  77.                 }
  78.                 wd_array[i].wd = wd;
  79.         }
  80.        

  81. for (;;)
  82. {   
  83.         index = 0;
  84.         len = 0;
  85.         while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));     
  86.   
  87.   while (index < len)
  88.                   {        
  89.                           event = (struct inotify_event *)(buf + index);      
  90.                           inotify_event_handler(event);        
  91.                           index += sizeof(struct inotify_event) + event->len;      
  92.                   }   
  93.         }          
  94.   

  95. for (i=0; i<MONITOR_NUM; i++)
  96.         inotify_rm_watch(fd, wd_array[i].wd);

  97. close(fd);

  98. return 0;
  99. }

复制代码

我以前写的一个玩的inotify程序

论坛徽章:
0
10 [报告]
发表于 2009-12-29 12:07 |只看该作者
你这种情况只用IN_CREATE就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP