Chinaunix

标题: 监控多个文件目录 [打印本页]

作者: jiangf    时间: 2009-12-28 10:50
标题: 监控多个文件目录
我现在要开发一个程序,程序的功能是监控30个目录。一旦该目录下有新文件就对新文件作处理,但这30个目录产生新文件的时间都不统一。我想用多线程的方式去监控处理这些目录,一个线程监控一个,如果这个目录下没有文件,该线程就休眠。但领导的意思是用单线程的方式来监控,这样会在维护上比较好控制。

大家看看有哪种办法更可靠方便,或者有啥更好的想法呢
作者: ubuntuer    时间: 2009-12-28 11:06
inotify
作者: flw    时间: 2009-12-28 11:09
你需要考虑四个问题:
1,启动的时候,对启动前已经存在的目录和文件如何处理;
2,运行的过程中,如果目录被删除了如何处理。
3,对性能的要求
4,对实时性的要求


可以用 inotify(7) 做,但是要考虑必要性,因为这会让程序变得复杂。
作者: c/unix    时间: 2009-12-28 11:12
提示: 作者被禁止或删除 内容自动屏蔽
作者: koolcoy    时间: 2009-12-28 11:20
如果采用轮询的方式,一个线程监控30个目录跟一个线程监控1个目录有多大的区别?
作者: lenky0401    时间: 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
作者: @sky    时间: 2009-12-28 15:02
还是inotify吧
作者: albcamus    时间: 2009-12-28 15:31
tripwire
作者: ubuntuer    时间: 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程序
作者: ubuntuer    时间: 2009-12-29 12:07
你这种情况只用IN_CREATE就可以了
作者: A.com    时间: 2009-12-29 13:40
一个进程轮询就可以了,没必要30个进程的。如果对文件的处理过程复杂需要较长时间的话,处理文件可以使用多进程,由监视进程来触发就行了
作者: jiangf    时间: 2009-12-29 15:33
明白了,谢谢大家,我先试试
作者: jiangf    时间: 2009-12-29 15:58
用inotify这种办法好是好,但是对内核有要求,至少要2.6.13版本的内核才可以。
我目前的服务器没有这么高的版本,而且升级内核的影响面太广,估计不会批下来。

有没有一种可以替代的方法呢,比如说用信号通知的方式




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2