I am working with linux and I have a directory which has sub-directories and there are files inside sub-directories. I have to monitor the changes in the file. In C++ I am using boost. I go through all the directories every 30 Seconds and check the last_write_time. Principally, it works. But every time this action is executed, my CPU goes nuts and I see 15%-25% CPU usage just for this program in top. I have read about inotify. If I use inotify would I have the more or less same CPU usage or would it be improved? Are there any good alternatives to what I am doing?
In Linux, the default monitor is inotify. By default, fswatch will keep monitoring the file changes until you manually stop it by invoking CTRL+C keys. This command will exit just after the first set of events is received. fswatch will monitor changes in all files/folders in the specified path.
inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.
As a conclusion of this article you should be aware of Inotify as an efficient way to trace events in the filesystem on Linux. Whereas polling introduces a delay in handling data the Inotify framework provides an option to handle, debug and monitor filesystem activities just as an event takes place.
In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file. When we add a watch to a file, we can monitor it. For example, we'll know when a process opens, modifies, reads closes, moves, or deletes the file.
When you use inotify, you do not require to poll for all files to check if there are changes. You get a callback system that notifies you when a watched file or directory got changed.
The kernel/filesystem already has this information, so the resource/CPU usage is not just moved to another application, it is actually reduced.
Monitor file system activity with inotify provides more details why to use inotify, shows its basic usage and helps you set it up.
http://linux.die.net/man/7/inotify this should help you to get rid of the problem you facing!
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