Search code examples
c++linuxfile-monitoring

Monitoring file changes in C++ on Linux


I am working with Linux, and I have a directory which has subdirectories and there are files inside subdirectories.

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? Is there a good alternative to what I am doing?


Solution

  • 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.