我正在进行一个监视文件夹和文件的项目,但我不知道inotify_add_watch是如何工作的。我在网上搜索fd做了什么,但我不懂手册页
fd参数是一个文件描述符,它引用了要修改其监视列表的inotify实例。
那是什么意思?
发布于 2020-12-25 03:48:32
fd是inotify_init()或inotify_init1()的返回值。
first, use inotify_init() or inotify_init1() to initialize an inotify
instance;
second, return a file descriptor (with a new inotify event queue) fd ;
third, use the returned fd in inotify_add_watch to monitor the file path
you want to monitor.示例代码:
int fd = inotify_init();
int wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);
for (;;)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, &fds, NULL, NULL, NULL) > 0)
{
int len, index = 0;
while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));
while (index < len)
{
event = (struct inotify_event *)(buf + index);
_inotify_event_handler(event);
index += sizeof(struct inotify_event) + event->len;
}
}
}
inotify_rm_watch(fd, wd);
return 0; 发布于 2020-12-31 02:37:00
文件描述符基本上是某些输入/输出资源的标识符。当您使用inotify时,您需要使用inotify_init初始化一个实例,该实例返回一个文件描述符,然后您可以使用inotify_add_watch将要查看的文件添加到实例中。
因此,在回答您的问题时,inotify_add_watch中的fd参数是inotify实例的文件描述符,由inotify_init调用返回。
https://stackoverflow.com/questions/65444989
复制相似问题