English 中文(简体)
由于读 () 块, 无法查看多个目录
原标题:Can t watch multiple directories because of read() block

我的问题是,我用无记名功能来观察多个目录,我用读()函数来读懂任何更改。我的观点是,我不能在“ 相同时间” (“ for” roll) 中查看全部目录,因为读() 函数停止程序直到当前所监视目录发生一些事情为止。

有简化的“主”代码 :

while (1){

    for(int i = 0; i < numberOfDirectories; i++){

        string fileEnd = get_event(fd[i], catalogs[i]).c_str());
        if(string != "") do some code;

    }
sleep(1);
}

获取事件返回已更改文件的路径时, fd[i] 是匿名的例子, 目录[i] 包含被监视目录的名称 。

也有一些代码 得到甚至func:

#define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
string get_event(int fd, string target)
{
    ssize_t len;    
    char buff[BUFF_SIZE] = {0};       

    len = read (fd, buff, BUFF_SIZE); 

在此点, 主“ for” 循环停止工作, 等待第一个目录发生一些事情。 我只想检查被监视目录中是否有变化, 而不是等待更改 。

帮助: <

最佳回答

调调

fcntl(fd, F_SETFL, O_NONBLOCK);

在读取(...) 函数之前, 函数将文件描述符强制到无阻塞模式 。

问题回答

如果您在 LINUX/ UNIX 中重写, 您可以使用 SELECT 来监测多个文件描述符以进行新的修改。 任何第一个修改都会打破屏蔽等候, 给您修改的描述符的数量, 允许您处理, 然后您再次监测它 。

它让您可以捕捉到任何变化, 不论它们使用哪个描述符, 只要您提供所有描述符 用于您感兴趣的位置, 在呼叫的描述符里 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签