【发布时间】:2019-08-10 17:43:49
【问题描述】:
我不明白 inotify 事件的进度。 我知道 inotify_init 是创建新的 inotify 实例。它返回文件描述符。 此时。文件描述符是关于什么的?
在我的代码中,函数 inotify_add_watch 如下所示
wd = inotify_add_watch ( inotifyFd, argv[j], IN_ALL_EVENTS),
被循环调用。
如果我在命令中再输入 2 个文件,则此循环将重复 2 次以上。然后变量 wd 将被覆盖。并且 intofiyFd 总是有相同的数字。因为这些不是数组。 那么,它如何区分另外 2 个文件呢?
我已经了解了
int main (int argc, char *argv[])
{
int inotifyFd, wd, j;
char buf[BUF_LEN];
ssize_t numRead;
char *p;
struct inotify_event *event;
inotifyFd = inotify_init();
for(j = 1; j < argc; j++)
{
wd = inotify_add_watch(inotifyFd, argv[j], IN_ALL_EVENTS);
printf("Watching %s using wd %d\n", argv[j], wd);
}
for(;;)
{
numRead = read(inotifyFd, buf, BUF_LEN);
if(numRead == 0)
fatal("read() from inotify fd returned 0!");
printf("Read %ld bytes from inotify fd\n", (long) numRead);
for( p = buf; p < buf+numRead; )
{
event = (struct inotify_event *) p;
displayInotifyEvent(event);
p+= sizeof(struct inotify_event) + event->len;
}
}
return 0;
}
.
./demoinotify dir1 dir2 &
[1] 5386
Watching dir1 using wd1
watching dir2 using wd2
【问题讨论】:
-
最后一个文件描述符被删除并返回一个新的。我不明白。 Inotify 将它需要的所有信息存储在内核中。