inotify + epoll实现热插拔检测

概述:

inotify检测目录下文件的新建与删除,epoll监测某个文件被写了还是被读了。
二者结合实现热插拔检测。

inotify介绍:

/*
    功能:   初始化inotify
    返回值:    inotify的文件描述符,以后的add操作将要用到
*/
 int inotify_init(void);
/*
    功能:   将某个目录加入到监控中
    输入参数: fd---初始化时得到的文件描述符
             pathname---要监控的路径
             mask------要监控的事件类型
             IN_ALL_EVENTS  (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | 
             IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | 
             IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF | 
             IN_MOVE_SELF)
*/
int inotify_add_watch(int fd, const char *pathname, uint32_t mask)

epoll介绍:

epoll实现多文件的读写监控。三部曲:epoll_create(),  epoll_ctl(), epoll_wait().

实例:

代码下载:

下载源码:https://git.coding.net/xxgui1992/Android-hotploggin.git

原文地址:https://www.cnblogs.com/xxg1992/p/6636365.html