Intotify观察者在LS或观看命令后停止工作
inotify watcher stops working after ls or watch commands
我从Internet上的许多示例中获取了有关如何使用Inotify的代码。
我尝试了以下实验:
1)在下面运行观察者
2)在单独的外壳中,CD纳入'/mypath'
中,为您正在观看的文件夹创建一些文件。例如,'date > output.txt'
更多次矿。
3)您将看到观察者的通知。
4)类型'ls /mypath'
(甚至'watch -n 1 /mypath'
)
5)在/我的路径中尝试'date > output.txt'
。您将不再看到观察者的通知。或者至少,这是我与Ubuntu 12/13测试时发生的情况。
关于如何修复它的任何想法?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>
#include <unistd.h>
#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
int main()
{
int length, i = 0, wd;
int fd;
char buffer[BUF_LEN];
/* Initialize Inotify*/
fd = inotify_init();
if ( fd < 0 ) {
perror( "Couldn't initialize inotify");
}
/* add watch to starting directory */
wd = inotify_add_watch(fd, "/mypath", IN_CLOSE_WRITE | IN_CLOSE_NOWRITE);
if (wd == -1)
{
printf("Couldn't add watch to %sn","/mypath");
}
else
{
printf("Watching:: %sn","/mypath");
}
/* do it forever*/
while(1)
{
i = 0;
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if ( event->mask & IN_CLOSE_WRITE) {
if (event->mask & IN_ISDIR)
printf( "The directory %s was Created.n", event->name );
else
printf( "The file %s was closed (write) with WD %dn", event->name, event->wd );
}
if ( event->mask & IN_CLOSE_NOWRITE) {
if (event->mask & IN_ISDIR)
printf( "The directory %s was Created.n", event->name );
else
printf( "The file %s was closed (nowrite) with WD %dn", event->name, event->wd );
}
i += EVENT_SIZE + event->len;
}
}
}
/* Clean up*/
inotify_rm_watch( fd, wd );
close( fd );
return 0;
}
您不应将i += EVENT_SIZE + event->len;
放入if ( event->len )
块中。如果事件的长度名称为零,则指针仍应由EVENT_SIZE
递增(如果您将该语句放在块之外,这将发生)。我认为您可能会在iNotify程序中看到一个无限的循环,这是第一个事件开始的,该活动恰好具有零长度的名称。(这正是ls
:目录正在打开,而不是其文件,因此name
字段中没有任何内容。)
您进入了一个永恒的循环,因为您不会在event->len == 0
时更改i
添加此:
else
i += EVENT_SIZE ;
如果if ( event->len == 0 )
事实证明,当您的程序停止工作时,它会吃掉所有CPU。我进行了一些更改,现在似乎可以使用。这是详细信息:
声明buf_len处理16个事件(您可以增加该值):
#define BUF_LEN (16 * (sizeof(struct inotify_event) + NAME_MAX + 1))
更改将事件处理为以下循环的while (i < length)
循环:
for ( p = buffer; p < buffer + length; ) {
struct inotify_event *event = ( struct inotify_event * ) p;
p += sizeof(struct inotify_event) + event->len;
if ( event->len ) {
/* SNIP */
}
}
p
变量应声明为char *
,您可以删除不再使用的i
。
相关文章:
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- C++代码停止工作错误使用cout内部函数
- LoadImage 在 Windows 更新后停止工作
- C++程序工作,但 Windows 显示"程序.exe已停止工作"
- VS 代码 C/C++ 智能感知在更新后停止工作
- 不使用与左右停止工作命名空间 std 的简单比较
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- 德拉吉诺 LG01-S 收到异常数据包并停止工作
- G++ 在生成文件中使用时停止工作
- 当循环在条件C++之前停止工作时
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- 正则表达式代码无缘无故停止工作
- 我的代码在第 4 个选择后停止工作
- 读取二进制文件大小的短自上周以来停止工作
- C++ 已停止工作静态映射函数
- Vscode 自动格式化代码在运行几次后停止工作
- mkdir 从命令行工作,但在使用 NFS 时C++失败
- 程序 使用链表插入数据时停止工作
- Intotify观察者在LS或观看命令后停止工作