Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding watches to Inotify in multi-threaded program

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?

like image 475
Andna Avatar asked Jun 17 '12 09:06

Andna


People also ask

What is inotify watches?

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 inotify works?

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.

What is inotify file descriptor?

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.


1 Answers

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.

like image 194
Syed H Avatar answered Sep 27 '22 07:09

Syed H