Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1846954
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: LINUX

2020-03-15 22:36:16

使用inotify API有以下几个关键步骤

1. 应用程序使用inotify_init()来创建一个inotify实例,该系统调用返回的文件描述符用于在后续操作中指代该实例
2. 应用程序使用inotify_add_watch()向inotify实例的监控列表添加条目
3. 为获得事件通知,应用程序需要针对inotify文件描述符执行read()操作。每次对read()的成功调用,都会返回一个或多个inotify_event结构,其中各自记录了处于inotify实例监控之下的某一路径名所发生的事件
4. 应用程序在结束监控时会关闭inotify文件描述符,这会清除与inotify实例相关的所有监控项
inotify监控为非递归

可以使用select(), poll(), epoll(), 以及由信号驱动的I/O来监控inotify文件描述符

inotify相关api如下:

点击(此处)折叠或打开

  1. #include <sys/inotify.h>
  2. int inotify_init(void);
  3. /*returns file descriptor on success, or -1 on error*/
  4. int inotify_add_watch(int fd, const char *pathname, uint32_t mask);
  5. /*returns watch descriptor on success, or -1 on error*/
  6. int inotify_rm_watch(int fd, uint32_t wd);
  7. /*returns 0 on success, or -1 on error*/
成功读取后返回的inotify_event结构如下所示:

点击(此处)折叠或打开

  1. struct inotify_event{
  2.     int wd;
  3.     uint32_t mask; /*bits describing event that occurred, 上表out列为Y的值*/
  4.     uint32_t cookie; /*cookie for related events (for rename())*/
  5.     uint32_t len; /*size of name field*/
  6.     char name[]; /*optional null-terminated filename */
  7. };
对inotify事件做排队处理,需要消耗内核内存。正因如此,内核会对inotify机制的操作施加各种限制,超级用户可以配置/proc/sys/fs/inotify路径中的三个文件来调整这个限制:
1. max_queued_events: 实例队列中的事件数量设置上限。一旦超出上限,系统会生成IN_Q_OVERFLOW事件
2. max_user_instances:对由每个真是用户ID创建的inotify实例数的限制
3. max_user_watches:对由每个真是用户ID创建的监控项数量的限制


阅读(1105) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~