Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monitoring file changes c++ linux

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?

like image 306
user1566277 Avatar asked Feb 19 '13 09:02

user1566277


People also ask

How will you monitor file change in Linux?

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.

What is inotify in Linux?

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.

Is inotify efficient?

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.

How do I monitor a folder in Linux?

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.


2 Answers

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.

like image 53
Veger Avatar answered Sep 19 '22 09:09

Veger


http://linux.die.net/man/7/inotify this should help you to get rid of the problem you facing!

like image 28
Saqlain Avatar answered Sep 20 '22 09:09

Saqlain