0

My problem is that I use inotify to watch multiple directories and I use read() function to read any changes. My point is that I cant watch all of this directories in "same time" ("for" loop), because read() function stop program until something happens to currently watched directory.

There is simplified "main" code:

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);
}

Where get_event return path to changed file, fd[i] is instance of inotify, catalogs[i] contain name of watched directory.

And there is some code of 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); 

At this point main "for" loop stop work and wait until something happens in first directory. I want to just check is there any changes in watched directory instead of waiting for changes.

Help :<

wendigu
  • 3
  • 1

2 Answers2

1

If you're in LINUX/UNIX, you can use SELECT to monitor multiple file descriptors for new changes. Whichever changes first will break the blocking wait, giving you the number of the changed descriptor, let you process, then you just monitor it again.

It lets you catch any changes regardless of which descriptor they're on as long as you provide all the descriptors for the locations you're interested in within the descriptor set of the call.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
0

Call

fcntl(fd, F_SETFL, O_NONBLOCK);

just before read(...) function to force your file descriptors into nonblocking mode.

mzet
  • 577
  • 2
  • 7