Monitoring file changes in C++ on Linux
Asked Answered
O

1

19

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?

Oud answered 19/2, 2013 at 9:40 Comment(2)
The performance will be vastly improved, as you would not have to go through all files and sub-directories. Instead you will get a notification when a file or directory has changed. Inotify is probably the best way to go on Linux.Spunk
Thanks for the input Joachim. I am gonna try that.Oud
S
22

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.

Scrap answered 19/2, 2013 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.