简体   繁体   中英

Monitoring files asynchronously

On Unix: I've been through FAM and Gamin , and both seem to provide a client/server file monitoring system. I would rather have a system where I tell the kernel to monitor some inodes and it pokes me back when events occur. Inotify looked promising at first on that side: inotify_init1 let me pass IN_NONBLOCK which in turn caused poll() to return directly. However I understood that I would have to call it regularly if I wanted to have news about the monitored files. Now I'm a bit short of ideas.

Is there something to monitor files asynchronously?

PS: I haven't looked on Windows yet, but I would love to have some answers about it too.

As Celada says in the comments above, inotify and poll are the right way to do this.

Signals are not a mechanism for reasonable asynchronous programming -- and signal handlers are remarkably dangerous for the inexperienced and even for the experienced. One does not use them for such purposes voluntarily.

Instead, one should structure one's program around an event loop (see http://en.wikipedia.org/wiki/Event-driven_programming for an overall explanation) using poll, select, or some similar system call as the core of your program's event handling mechanism.

Alternatively, you can use threads, or threads plus an event loop.

However interesting are you answers, I am sorry but I can't accept a mechanism based on blocking calls on poll or select, when the question states “asynchronously”, regardless of how deep it is hidden.

On the other hand, I found out that one could manage to run inotify asynchronously by passing to inotify_init1 the flag IN_NONBLOCK . Signals are not triggered as they would have with aio , and a read call that would block blocking would set errno to EWOULDBLOCK instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM