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
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include <sys/inotify.h>
typedef struct wd_name {
int wd;
char * name;
}WD;
#define MONITOR_NUM 3
char* files[MONITOR_NUM] = {
"./a",
"./b",
"./c"
};
WD wd_array[MONITOR_NUM];
static void inotify_event_handler(struct inotify_event *event)
{
int i = 0;
if (event->mask & IN_ISDIR)
printf("Object type: %s\n", "Direcotory");
else
printf("Object type: %s\n", "file");
for (i=0; i<MONITOR_NUM; i++)
{
if (event->wd != wd_array[i].wd)
continue;
printf("Object name: %s\n", wd_array[i].name);
break;
}
if ( event->mask & IN_CREATE )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was created.\n", event->name );
else
printf( "The file %s was created.\n", event->name );
}
else if ( event->mask & IN_DELETE )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was deleted.\n", event->name );
else
printf( "The file %s was deleted.\n", event->name );
}
else if ( event->mask & IN_MODIFY )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was modified.\n", event->name );
else
printf( "The file %s was modified.\n", event->name );
}
}
int main(int argc, char **argv)
{
int fd = 0;
int wd = 0;
int i = 0;
int len = 0;
int index = 0;
unsigned char buf[1024] = {0};
struct inotify_event *event = {0};
fd = inotify_init();
if (fd < 0) {
printf("Fail to initialize inotify.\n");
return -1;
}
for (i=0; i<MONITOR_NUM; i++) {
wd_array[i].name = files[i];
wd = inotify_add_watch(fd, wd_array[i].name, IN_CREATE | IN_MODIFY | IN_DELETE);
if (wd < 0) {
printf("Can't add watch for %s.\n", wd_array[i].name);
return -1;
}
wd_array[i].wd = wd;
}
for (;;)
{
index = 0;
len = 0;
while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));
while (index < len)
{
event = (struct inotify_event *)(buf + index);
inotify_event_handler(event);
index += sizeof(struct inotify_event) + event->len;
}
}
for (i=0; i<MONITOR_NUM; i++)
inotify_rm_watch(fd, wd_array[i].wd);
close(fd);
return 0;
}
复制代码
我以前写的一个玩的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