linux有没有办法像select()那样轮询一个目录中是否有文件
我现在的办法是自己写个循环每sleep若干秒访问一下目录
现在想改变一下方法,像select判断文件描述符那样...
谢谢
[解决办法]
up
[解决办法]
遍历目录,查找是否有指定的文件。
遍历目录参考:
linux下面的
A Demo written by camelrain
/*
the program find a file from current directory or your defined directory
commond optinon [path] filename
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define LENGTH 256
/* just if is a directory*/
static int IsDir (char * name);
/* search target file, arg1 is current path, arg2 is target file*/
static void search_file (char * path, char * name);
static int search_flag=0;
/*just if is a directory*/
static int IsDir (char * name) {
struct stat buff;
if (lstat(name,&buff) <0)
return 0; //if not exist name ,ignore
/*if is directory return 1 ,else return 0*/
return S_ISDIR(buff.st_mode);
}
/*search target file*/
static void search_file (char * path, char * name) {
DIR *directory;
struct dirent * dir_entry;
char buffer[LENGTH];
if ((directory=opendir(path)) == NULL) {
fprintf(stderr, "%s ", path);
perror( " ");
return;
}
while (dir_entry=readdir(directory)) {
if (!strcmp(dir_entry-> d_name, ". ")||!strcmp(dir_entry-> d_name, ".. ")) {
/* do nothing*/
}
else {
/* if is boot directory add "/ " */
if ((strcmp(path, "/ "))==0)
sprintf(buffer, "%s%s ",path,dir_entry-> d_name);
/* if is not boot directory do not add "/ "*/
else
sprintf(buffer, "%s/%s ",path,dir_entry-> d_name);
//printf( "Now file : %s\n ",buffer);
if (IsDir(buffer))
search_file (buffer , name);
else {
if (strcmp(dir_entry-> d_name,name)==0)
{
printf( "%s\n ",buffer);
search_flag=1;
}
}
}
}
closedir(directory);
}
int main(int argc, char *argv[])
{
static char * current_dir;
static char * filename;
int length;
if (argc==1) {
printf( "A few parameters!!\n ");
return 0;
}
if (argc==2) {
current_dir=(char * )getcwd(current_dir,LENGTH);
filename=argv[1];
}
if (argc==3) {
length=strlen(argv[1]);
if (length> 1 && (argv[1][length-1]== '/ ')) {
argv[1][length-1]= '\0 ';
//printf( "%d\n ",strlen(argv[1]));
}
current_dir=argv[1];
filename=argv[2];
}
search_file(current_dir,filename);
if (!search_flag)
printf( "Not found this(%s) file!\n ",filename);
return 0;
}
------解决方案--------------------
lz能否将需求场景再说的细一点,
1。比如说你是想等到一个目录有文件生成后触发一个事件让系统捕获,这样的处理就考虑事件触发机制。假如以多线程的软件结构来描述一下解决方案,可以让子线程发消息告诉主线程等。。。
2。说一下对select的理解,它是由操作系统来控制对几个文件描述符上的数据收发,是一个典型的事件触发模型。
3。 对你说的sleep()方案,我不太赞同,(1)。无故休眠对系统资源是有负面影响。(2)。如果正好在你sleep的间隔发生了文件的生成事件而你偏等到睡晚才处理,会耽误一定实时性。关于这一点,你可以参考一下windows下多线程中用事件处理多线程间的同步问题。
当然,如果你的系统对实时性要求不是非常高,休眠几秒也无妨,而且如果引入多线程也有系统资源上的损耗,你权衡考虑一下。
欢迎各位批评指正!
[解决办法]
去 IBM dW 或者直接 Google "inotify " :)
"dnotify ", if you happen to find, is the older way based on UNIX signals and is deprecated by inotify.
[解决办法]
> > 无故休眠对系统资源是有负面影响
"sleep " handles back CPU resources to OS, which allows other processes / threads who need time running to take their chance, which is really the "polite " way to do. On the other hand, busy loop waiting is to be avoided because by doing busy looping like "for (;;) check_condition() && break; " CPU is acquired all the time hence a waste of computing resources.
Best way to utilize computing resources is to handle back to OS since only OS has the global view of the system.
[解决办法]
参看SGI的FAM程序库。。。
一个目录或者文件有改变的时候,它都能给你通知。对你这个功能需求,用FAM来做就太简单了。