- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我目前在我的 C 代码中使用 inotify() 系统来监视文件系统中某些目录的事件。
现在,使用这些东西之一的过程如下。你取一个整数(比如 event_notifier),使用 inotify_init() 将它变成一个 inotify 描述符,就像这样
event_notifier=inotify_init();
现在,假设我想监视多个目录中的事件。然后我将在这些目录上向这个 event_notifier 添加监视
wd1 = inotify_add_watch(event_notifier,"/../path..to..directory1/../",IN_ALL_EVENTS);
wd2 = inotify_add_watch(event_notifier,"/../path..to..directory2/../",IN_ALL_EVENTS);
wd3 = inotify_add_watch(event_notifier,"/../path..to..directory3/../",IN_ALL_EVENTS);
. . . .
wdn = inotify_add_watch(event_notifier,"/../path..to..directoryn/../",IN_ALL_EVENTS);
现在,我可以在多个目录上添加监视。这些调用中的每一个都返回一个“监视描述符”(上面的 wd1、wd2、wd3.. wdn)。每当任何目录中发生事件时,inotify 系统都会向 inotify 文件描述符 event_notifier 发送一个事件以及对应于该特定“监视目录”的监视描述符(wd1、wd2...wdn)
当事件发生时,我可以读取 event_notifier 中的 struct inotify_event 数组。这个 inotify_event 结构有以下字段:
struct inotify_event
{
int wd; //Watch descriptor
...
uint32_t len; //Size of 'name' field
char name[]; //null terminated name
}
要读取事件,你只需要做
read(event_notifier, buffer, sizeof(buffer))
struct inotify_event* event;
event=(struct inotify_event*)buffer; //Assuming only one event will occur
我想知道通知来自哪个目录。但是当我 stat() watch 描述符时,它什么也没给我
struct stat fileinfo;
fstat(event->wd, &fileinfo);
printf("\n Size of file is %l",fileinfo_st.size);
甚至/proc/self/fd/event->fd 上的 readlink() 也没有产生任何文件名。
char filename[25];
readlink("/proc/self/fd/event-wd",filename,sizeof(filename));
printf("\n The filename is %s",filename);
我有两个问题:
1) watch 描述符究竟指向什么? 它有什么好处?
2) 我如何知道通知来自哪个目录?
最佳答案
What is the watch descriptor pointing to exactly ? What is it good for ?
监视描述符不是文件系统对象或文件描述符。它是 inotify 子系统用来将事件链接到 watched 资源的资源描述符,并使您可以在删除它们时指定某些 watches。
您还应该注意,可能的“打开”监视描述符的数量在系统上是有限的。您可以使用以下方法获得最大值:
cat /proc/sys/fs/inotify/max_user_watches
如果您出于任何原因需要超过此值,您可以使用以下方式设置值:
sudo sysctl -w fs.inotify.max_user_watches=XXXXXX
How can I tell which directory the notification is coming from ?
仅使用 inotify 扩展无法从事件结构中获取文件(目录)的完整路径。您的应用程序代码将需要特殊的查找表来存储监视描述符和完整路径名之间的链接。我在 PHP 中做过一次,因为我也觉得我需要它。您可以查看 Github 上的代码.就像我说的,它是 PHP,但它可能有助于理解我在做什么。 (inotify 系统调用签名在 PHP 和 C 中是相同的)
关于c - watch 描述符到底是什么? (Linux inotify 子系统),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24342156/
如何解决以下问题?我正在使用 compass 。 Too many open files - Failed to initialize inotify: the user limit on th
我最终遇到了这样一种情况:一个目录正在被 inotify 监视,以触发一个不存在的进程。 我只是想阻止inotify监视目录,但是经过搜索和reading the man page我找不到如何做到这一
1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 1.2 实现实时
我正在尝试使用 inotify 递归地查看目录,这意味着,每当在此目录或其中一个子目录中创建目录或文件时,我都希望收到通知。程序开始时主目录为空。 我基本上是从这里复制代码进行测试:https://g
我正在编写一个基于源代码的监控文件程序:https://github.com/kvikas/file-monitor-service/blob/master/ 我的程序使用 boost::asio::
我是 Fedora 用户,目前正在使用 inotify-java 来实现目录轮询。我有以下代码片段: InotifyEventListener inel = new InotifyEventListe
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我正在用 C 语言编写一个简单的线程来监视目录内文件的修改。该目录中有多个文件,其中一些文件经常更改。我想监视一些特定文件。 我仔细阅读了Linux手册,但没有找到答案谢谢问候帕特里夏 最佳答案 您无
我的程序使用 inotify用于监视文件的更改。我的代码如下: fd = inotify_init(); wd = inotify_add_watch(fd, "./test.txt"
我正在繁忙的目录上使用inotify(文件不断生成)。我希望捕获所有这些事件的 IN_CLOSE_WRITE 事件。 所以我尝试了这样的事情。 fd = inotify_init(); inotify
我尝试在线程内的 C++ 中使用 inotify但是 select 是阻塞的,所以当我的应用程序退出时我永远不能离开线程 我如何创建 inotify watch fd=inotify_init1(IN
我想监控一个文件夹。 每次弹出通知时,我都想运行系统命令行。使用系统命令时出现问题。每个新事件弹出 3 次,尽管它应该弹出一次。编辑:谢谢你重播。我发现了这个错误。系统执行了一个位于受监控文件夹内的文
我想监控某些目录中新文件的创建并阅读有关 inotify 的链接.我喜欢这个实现并使用了它。然而,在我的例子中,我想监控一个最多有 3 级子目录的目录。 我的想法是每次创建一个新目录时添加一个 wat
删除同一目录下的文件时,事件丢失。 inotify监控目录:./test删除命令:rm -rf ./test/* 文件数:20; 但是事件通知12,这是怎么回事? int fileDescripter
所以我使用 inotifywait 运行了这个脚本。一台服务器将图像放入位于/var/nfs/device_images 的主机服务器上的 NFS 文件夹中。 (工作文件夹上的 chmod 是 777
例如,假设您有一个以下列形式使用 inotifywait 的 shell 脚本 inotifywait -m -e create /home/user1/*/* 本质上,您是在要求它监控 user1
我在 Linux 上使用 inotify 来监视目录。我的目录有 50 个子目录,目录树有 20 层深。到目前为止,我能够监视目录及其所有子目录。我使用 C++ STL vector 来保存每个目录路
我的 Linux 服务器上有 inotify。我在网上查找了大量关于如何使用 inotify 的帖子,并找到了一个示例 C 代码,用于监视文件创建/删除的目录。它在本地目录和 nfs direcory
我正在寻找一个使用 inotify 的简单、简洁的示例gem 来检测目录的更改。 它缺少示例。 最佳答案 examples/watcher.rb 中有一个示例.该链接指向 aredridel 的 re
所以我有一个文件系统,它正在下载一些数据,将其存储在内存中,并仅将已完成的下载作为文件呈现给用户。但是,每次下载可能需要一些时间才能完成,因此我不希望用户必须等待所有文件完成下载。我执行此操作的方法是
我是一名优秀的程序员,十分优秀!