我的問題是我使用inotify觀看多個目錄,並使用read()函數來讀取任何更改。我的觀點是,我不能在「同一時間」(「for」循環)中觀察所有這些目錄,因爲read()函數會停止程序,直到目前觀察到的目錄出現問題。由於讀取()塊無法觀看多個目錄
有被簡化的 「主」 代碼:
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);
}
凡get_event返回更改後的文件路徑的FD的inotify,目錄[i]爲實例[I]含有所在目錄的名稱。
而且有get_even 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); 工作!非常感謝 :) – wendigu