I wanted to use inotify for monitoring some files in my C program.
I am wondering if it is safe to have one thread reading from inotify descriptor (the one returned by inotify_init
) thus blocking until some event happens, during this waiting there would be a possibility of adding new file to watch queue using inotify_add_watch
during the other thread waiting period.
Do I need to synchronize those actions or is it safe to do such thing?
Inotify Watch helps to keep track of the file changes under the directories on "watch" and report back to the application in a standard format using the API calls. We can monitor multiple file events under the watched directory using the API calls.
How does Inotify Work? The Inotify develops a mechanism for monitoring file system events, which watches individual files & directories. While monitoring directory, it will return events for that directory as well as for files inside the directory.
The inotify API identifies events via watch descriptors. It is the application's responsibility to cache a mapping (if one is needed) between watch descriptors and pathnames. Be aware that directory renamings may affect multiple cached pathnames.
Don't have the exact answer, but I do know from experience that you can't even open files in another thread without triggering the read() in the thread you are using inotify. I recall reading that you need to use inotify_init1() along with the IN_CLOEXEC flag to allow file io in other threads. I'm not sure if that means you can actually use inotify in more than one thread simultaneously though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With