简体   繁体   English

监控 Linux 上 C++ 中的文件更改

[英]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.我正在使用 Linux,我有一个包含子目录的目录,并且子目录中有文件。

I have to monitor the changes in the file.我必须监视文件中的更改。 In C++ I am using Boost .在 C++ 我正在使用Boost I go through all the directories every 30 seconds and check the last_write_time .我每 30 秒通过所有目录 go 并检查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.但是每次执行此操作时,我的 CPU 都会发疯,我看到仅在顶部的该程序的 CPU 使用率为 15%-25%。 I have read about inotify .我已经阅读过inotify If I use inotify , would I have the more or less same CPU usage or would it be improved?如果我使用inotify ,我的 CPU 使用率会或多或少相同还是会得到改善? Is there a good alternative to what I am doing?我正在做的事情有什么好的选择吗?

When you use inotify, you do not require to poll for all files to check if there are changes. 使用inotify时,不需要轮询所有文件以检查是否有更改。 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. 内核/文件系统已经有了这些信息,因此资源/ CPU的使用不仅仅是移动到另一个应用程序,它实际上已经减少了。

Monitor file system activity with inotify provides more details why to use inotify, shows its basic usage and helps you set it up. 使用inotify监视文件系统活动提供了有关使用inotify的更多详细信息,显示了其基本用法并帮助您进行设置。

http://linux.die.net/man/7/inotify这应该可以帮助您摆脱面临的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM