导航

inotify

现在的使用是用于程序和脚本的通信。

  • 头文件
#include <sys/inotify.h>

 

  • 初始化函数
int inotify_init1(int flags)

支持flag,可设置为IN_NONBLOCK,代码简化

返回为fd

  • 监听使能,监听的文件必须存在
int inotify_add_watch(int fd, const char *pathname, u32 mask)

mask设置监听类型,如IN_MODIFY

fd为初始化的fd

返回为wd

  • 解析
    • server端创建的socket注册到epoll,当有EPOLLIN事件时,不会去读该fd,而是做acccept操作
    • inotify同样fd注册到epoll,但是有EPOLLIN事件时,读该fd,获取struct inotify_event实例,再做相应操作
  • inotify_event
/*
struct inotify_event {
    int wd;
    u32 mask;
    u32 cookie;
    u32 len;
//char name[];
};
*/

#include <stdio.h>

#include <sys/inotify.h>

int main()
{
    int size = sizeof(struct inotify_event);

        printf("size :%d\n", size);
        return 0;
}

输出为4,即len默认为0

posted on 2021-04-20 09:19  toughcactus  阅读(129)  评论(0编辑  收藏  举报