输入系统:epoll & inotify
一、epoll
作用:检测一个或多个文件的可读、可写等属性变化:
代码示例:
#include <sys/epoll.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> #if 0
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t; #endif #define DATA_MAX_LEN 500 /* usage: epoll <file1> [file2] [file3] ... */ int add_to_epoll(int fd, int epollFd)
{
int result;
struct epoll_event eventItem;
memset(&eventItem, , sizeof(eventItem));
eventItem.events = EPOLLIN;
eventItem.data.fd = fd;
result = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &eventItem);
return result;
} void rm_from_epoll(int fd, int epollFd)
{
epoll_ctl(epollFd, EPOLL_CTL_DEL, fd, NULL);
} int main(int argc, char **argv)
{
int mEpollFd;
int i;
char buf[DATA_MAX_LEN]; // Maximum number of signalled FDs to handle at a time.
static const int EPOLL_MAX_EVENTS = ; // The array of pending epoll events and the index of the next event to be handled.
struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; if (argc < )
{
printf("Usage: %s <file1> [file2] [file3] ...\n", argv[]);
return -;
} /* epoll_create */
mEpollFd = epoll_create(); /* for each file:
* open it
* add it to epoll: epoll_ctl(...EPOLL_CTL_ADD...)
*/
for (i = ; i < argc; i++)
{
//int tmpFd = open(argv[i], O_RDONLY|O_NONBLOCK);
int tmpFd = open(argv[i], O_RDWR);
add_to_epoll(tmpFd, mEpollFd);
} /* epoll_wait */
while ()
{ int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, -);
for (i = ; i < pollResult; i++)
{
printf("Reason: 0x%x\n", mPendingEventItems[i].events);
int len = read(mPendingEventItems[i].data.fd, buf, DATA_MAX_LEN);
buf[len] = '\0';
printf("get data: %s\n", buf);
//sleep(3);
} } return ;
}
二、inotify
作用: 监控一个目录下文件的增加、删除事件:
代码示例:
#include <unistd.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <string.h>
#include <errno.h> /*
*参考: frameworks\native\services\inputflinger\EventHub.cpp
*/ /*Usage: inotify <dir> */ int read_process_inotify_fd(int fd)
{
int res;
char event_buf[];
int event_size;
int event_pos = ;
struct inotify_event *event; /* read */
res = read(fd, event_buf, sizeof(event_buf)); if(res < (int)sizeof(*event)) {
if(errno == EINTR)
return ;
printf("could not get event, %s\n", strerror(errno));
return -;
} /* process
* 读到的数据是1个或多个inotify_event
* 它们的长度不一样
* 逐个处理
*/ while(res >= (int)sizeof(*event)) {
event = (struct inotify_event *)(event_buf + event_pos);
//printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
if(event->len) {
if(event->mask & IN_CREATE) {
printf("create file: %s\n", event->name);
} else {
printf("delete file: %s\n", event->name);
}
}
event_size = sizeof(*event) + event->len;
res -= event_size;
event_pos += event_size;
}
return ;
} int main(int argc, char **argv)
{
int mINotifyFd;
int result; if (argc != )
{
printf("Usage: %s <dir>\n", argv[]);
return -;
} /* inotify_init */ mINotifyFd = inotify_init(); /* add watch */
result = inotify_add_watch(mINotifyFd, argv[], IN_DELETE | IN_CREATE); /* read */
while ()
{
read_process_inotify_fd(mINotifyFd);
} return ;
}
三、inotify和epoll的综合应用:
代码示例:
#include <sys/epoll.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/inotify.h>
#include <stdlib.h>
#include <errno.h> #define DATA_MAX_LEN 500
#define MAX_FILES 1000 static char *base_dir;
static char *epoll_files[MAX_FILES]; #if 0
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t; #endif /* usage: epoll <file1> [file2] [file3] ... */ int add_to_epoll(int fd, int epollFd)
{
int result;
struct epoll_event eventItem;
memset(&eventItem, , sizeof(eventItem));
eventItem.events = EPOLLIN;
eventItem.data.fd = fd;
result = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &eventItem); //添加一个文件句柄到epoll监测列表
return result;
} void rm_from_epoll(int fd, int epollFd)
{
epoll_ctl(epollFd, EPOLL_CTL_DEL, fd, NULL);
}
//通过文件名找到epoll监测列表中对应该文件的下标
int get_epoll_fd_for_name(char *name)
{
int i;
char name_to_find[];
sprintf(name_to_find, "%s/%s", base_dir, name); for (i = ; i < MAX_FILES; i++)
{
if (!epoll_files[i])
continue; if (!strcmp(epoll_files[i], name_to_find))
return i;
}
return -;
} /*
*参考: frameworks\native\services\inputflinger\EventHub.cpp
*/ /*Usage: inotify <dir> */ int read_process_inotify_fd(int mINotifyFd, int mEpollFd)
{
int res;
char event_buf[];
int event_size;
int event_pos = ;
struct inotify_event *event; /* read */
res = read(mINotifyFd, event_buf, sizeof(event_buf)); //返回值是一个或多个inotify_event总和大小 if(res < (int)sizeof(*event)) {
if(errno == EINTR)
return ;
printf("could not get event, %s\n", strerror(errno));
return -;
} /* process
* 读到的数据是1个或多个inotify_event
* 它们的长度不一样
* 逐个处理
*/ while(res >= (int)sizeof(*event)) {
event = (struct inotify_event *)(event_buf + event_pos);
//printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
if(event->len) {
if(event->mask & IN_CREATE) {
printf("create file: %s\n", event->name);
char *name = malloc();
sprintf(name, "%s/%s", base_dir, event->name);
int tmpFd = open(name, O_RDWR); printf("add to epoll: %s\n", name);
add_to_epoll(tmpFd, mEpollFd); epoll_files[tmpFd] = name; } else {
printf("delete file: %s\n", event->name);
int tmpFd = get_epoll_fd_for_name(event->name);
if (tmpFd >= )
{
printf("remove from epoll: %s/%s\n", base_dir, event->name);
rm_from_epoll(tmpFd, mEpollFd);
free(epoll_files[tmpFd]);
}
}
}
event_size = sizeof(*event) + event->len; //一个event大小=结构体大小+所含数据长度
res -= event_size;
event_pos += event_size;
}
return ;
} int main(int argc, char **argv)
{
int mEpollFd;
int i;
char buf[DATA_MAX_LEN];
int mINotifyFd;
int result; // Maximum number of signalled FDs to handle at a time.
static const int EPOLL_MAX_EVENTS = ; // The array of pending epoll events and the index of the next event to be handled.
struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; if (argc != )
{
printf("Usage: %s <tmp>\n", argv[]);
return -;
} base_dir = argv[]; /* epoll_create */
mEpollFd = epoll_create(); //创建epoll文件句柄 /* inotify_init */
mINotifyFd = inotify_init(); //创建inotify文件句柄,可用epoll监测 /* add watch */
result = inotify_add_watch(mINotifyFd, base_dir, IN_DELETE | IN_CREATE); //设置监测inotify文件句柄 add_to_epoll(mINotifyFd, mEpollFd); //使用epoll监测inotify文件句柄 /* epoll_wait */
while ()
{
int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, -); //当文件有数据可读时返回
for (i = ; i < pollResult; i++)
{
if (mPendingEventItems[i].data.fd == mINotifyFd) //当监测到的文件句柄为mINodifyFd(目录)时说明有文件创建/删除
{
read_process_inotify_fd(mINotifyFd, mEpollFd); //根据event->name 添加/移除 对该文件的监测
}
else
{
printf("Reason: 0x%x\n", mPendingEventItems[i].events);
int len = read(mPendingEventItems[i].data.fd, buf, DATA_MAX_LEN);
buf[len] = '\0';
printf("get data: %s\n", buf);
//sleep(3);
}
} } return ;
}
输入系统:epoll & inotify的更多相关文章
- Android系统--输入系统(一)必备的Linux知识_inotify和epoll
Android系统--输入系统(一)必备的Linux知识_inotify和epoll 引入 1. 笔记本电脑插入外接键盘,两个键盘都可以使用 a. 键盘即插即用--如何检测键盘的接入和拔出 hotpl ...
- 10.1、android输入系统_必备Linux编程知识_inotify和epoll
1. inotify和epoll 怎么监测键盘接入与拔出? (1)hotplug机制:内核发现键盘接入/拔出==>启动hotplug进程==>发消息给输入系统 (2)inotify机制:输 ...
- [Android] 输入系统(一)
Android输入系统是人与机器交互最主要的手段.我们通过按键或者触碰屏幕,会先经由linux产生中断,进行统一的处理过后,转换成Android能识别的事件信息,然后Android的输入系统去获取事件 ...
- Android系统--输入系统(八)Reader线程_使用EventHub读取事件
Android系统--输入系统(八)Reader线程_使用EventHub读取事件 1. Reader线程工作流程 获得事件 size_t count = mEventHub->getEvent ...
- Android系统--输入系统(二)必备Linux知识_实现inotify_epoll.c
Android系统--输入系统(二)必备Linux知识_实现inotify_epoll.c 课后作业 1. 编写 inotify_epoll.c, 用它来监测tmp/目录: 有文件被创建/删除, 有文 ...
- 《深入理解Android 卷III》第五章 深入理解Android输入系统
<深入理解Android 卷III>即将公布.作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白.即Android Framework中和UI相关的部分. ...
- 10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init(& ...
- Android系统--输入系统(十一)Reader线程_简单处理
Android系统--输入系统(十一)Reader线程_简单处理 1. 引入 Reader线程主要负责三件事情 获得输入事件 简单处理 上传给Dispatch线程 InputReader.cpp vo ...
- Android系统--输入系统(十六)APP跟输入系统建立联系_InputChannel和Connection
Android系统--输入系统(十六)APP跟输入系统建立联系_InputChannel和Connection 0. 核心:socketpair机制 1. 回顾Dispatch处理过程: 1.1 放入 ...
- 10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
12. 输入系统_APP跟输入系统建立联系_InputChannel和Connection核心: socketpair // 第9课第3节_输入系统_必备Linux编程知识_任意进程双向通信(scok ...
随机推荐
- window如何安装redis服务、卸载redis服务和启动redis服务
window如何安装redis服务.卸载redis服务和启动redis服务 一.总结 一句话总结:github上下载,解压,命令行运行(redis-server.exe redis.windows.c ...
- layui 日期插件onchange事件失效的方法
laydate.render({ elem:'#text1',//制定元素 type:'date', //range:true,//开启左右面板 min:'2017-09-1',// max:'201 ...
- Docker微容器+微服务将颠覆传统的软件架构
从我的观点看:Docker是一个微容器,一个云计算的微PaaS容器,类似JVM但比其更强大的容器,直接基于Linux内核,支持各种语言,它比VM虚拟机更加轻量,能够在Linux或云计算IaaS等平台上 ...
- 20165327《Java程序设计》实验一 Java开发环境的熟悉 实验报告
20165327<Java程序设计>实验二 <Java面向对象程序设计>实验报告 实验二 <Java面向对象程序设计> 一.实验报告封面 课程:Java程序设计 班 ...
- every day a practice —— morning(6)
"Nearly one in five job ads for China's 2018 national civil service called for 'men only' or 'm ...
- JSON和Serialize数据格式的对比
1.相同点: 都是把其它数据类型转换为可传输的字符串 都是结构性数据 2.不同点: JSON比Serialize序列后的格式要简洁 Serialize序列化的数据格式保存数据原有类型 3.扩展 JSO ...
- Android Studio 一直卡在building解决办法
1.随便找一个你能运行的as项目 2.打开gradle-wrapper.properties,文件目录:项目/gradle/wrapper/gradle-wrapper.properties 3.复制 ...
- Mysql中Join用法及优化
Join的几种类型 笛卡尔积(交叉连接) 如果A表有n条记录,B表有m条记录,笛卡尔积产生的结果就会产生n*m条记录.在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者直接用f ...
- Xor-MST CodeForces - 888G (最小生成树,分治)
大意: n结点无向完全图, 给定每个点的点权, 边权为两端点异或值, 求最小生成树
- Python mongoDB读取
class db_class(): def __init__(self): mongo_DB='test1' self.mongo_TABEL='test' client=pymongo.MongoC ...