Monitoring file changes

Monitoring files is a process often needed in applications. The most demanding applications for such a feature are media indexers. They need to know when some multimedia files have changed, appeared, or disappeared so that they re-index them and update their associated metadata. Another usual use case is simply monitoring a configuration file so that changes can be applied dynamically instead of requiring a restart of the application.

There are two ways to monitor files changes: either by directly polling for modifications in the file, or via the operating system providing some APIs to be notified when files are updated. The first solution has the advantage of being more portable. A solution that works on any system simply consists of reading the contents of the file regularly, and checking for any changes in it. Clearly this is a waste of resources because, to be practical, such a polling mechanism must occur quite often (every 10 seconds for example) while configuration file changes occur rarely. Another solution to avoid reading the whole file is checking for its modification time. This is easier than reading the whole contents, but more of a workaround for the lack of a dedicated solution.

The Linux kernel contains a feature dedicated to file monitoring—inotify. The principles of inotify are very simple—inotify allows you to register for modifications to a file or directory. Whenever a change occurs, the kernel notifies the application of these changes. This event-based mechanism is a perfect fit for a reactive application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.126.56