zoukankan      html  css  js  c++  java
  • 安卓输入子系统之inotify与epoll机制【学习笔记】【原创】

    平台信息:
    内核:linux3.1.0
    系统:android5.0
    平台:tiny4412

    作者:庄泽彬(欢迎转载,请注明作者)

    说明: 韦老师的安卓视频学习笔记

    一、在安卓的输入子系统中如何监听文件的产生以及监听文件是否有数据的输入,文件的监听主要使用的是inotify机制来监听文件的创建以及删除。使用epoll可以用来监听文件是否有数据的变化。下面针对这两种机制分别编程,简单的了解以及如何使用.

    二、使用inotify监听文件的创建以及删除.

    2.1我们先来看看现象之后在来看看具体的代码是如何实现的把。

    在后台运行的inotify可以检测到文件的创建以及删除。

    2.2代码的实现

     1 #include <unistd.h>
     2 #include <stdio.h>
     3 #include <sys/inotify.h>
     4 #include <string.h>
     5 #include <errno.h>
     6 
     7 
     8 int read_process_inotify_fd(int fd)
     9 {
    10     char event_buf[512];
    11     int event_size;
    12     int event_pos = 0;
    13     int res;
    14     struct inotify_event *event;
    15     
    16 
    17     res = read(fd, event_buf, sizeof(event_buf));
    18     if(res < (int)sizeof(*event)) {
    19         if(errno == EINTR)
    20             return 0;
    21         printf("could not get event, %s
    ", strerror(errno));
    22         return -1;
    23     }
    24 
    25     while(res >= (int)sizeof(*event)) {
    26         event = (struct inotify_event *)(event_buf + event_pos);
    27         //printf("%d: %08x "%s"
    ", event->wd, event->mask, event->len ? event->name : "");
    28         if(event->len) {
    29             if(event->mask & IN_CREATE) {
    30                 //openDeviceLocked(devname);
    31                 printf("Create file: %s
    ",event->name);
    32             } else {
    33                 //closeDeviceByPathLocked(devname);
    34                 printf("delete file: %s
    ",event->name);
    35             }
    36         }
    37         event_size = sizeof(*event) + event->len;
    38         res -= event_size;
    39         event_pos += event_size;
    40     }
    41 
    42     return 0;
    43 
    44 }
    45 
    46 int main(int argc,char **argv)
    47 {
    48     int mINotifyFd;
    49     int result;
    50 
    51     if (argc != 2){
    52         printf("Usage: %s <dir> 
    ",argv[0]);
    53         return -1;
    54     }
    55 
    56     mINotifyFd = inotify_init();
    57     if (mINotifyFd <= 0){
    58         printf("Error inotify_init
    ");
    59         return -1;
    60     }
    61 
    62     
    63     result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE);
    64 
    65     while(1){
    66         read_process_inotify_fd(mINotifyFd);
    67     }
    68     
    69     
    70 
    71     return 0;
    72 }

    编译命令:gcc -o inotify inotify.c,之后按照2.1截图的步骤运行即可。

    三、使用epoll来监听文件是否有数据的写入.

    3.1代码的具体实现如下:

     1 #include <sys/epoll.h>
     2 #include <stdio.h>
     3 #include <unistd.h>
     4 #include <sys/types.h>
     5 #include <sys/stat.h>
     6 #include <fcntl.h>
     7 #include <string.h>
     8 
     9 
    10 static const int EPOLL_SIZE_HINT = 8;
    11 static const int EPOLL_MAX_EVENTS = 16;
    12 
    13 #define DATA_MAX_LEN 512
    14 
    15 
    16 int add_to_epoll(int fd,int epollfd)
    17 {
    18     int result;
    19     struct epoll_event eventItem;
    20     
    21     memset(&eventItem, 0, sizeof(eventItem));
    22     eventItem.events = EPOLLIN;
    23     eventItem.data.fd = fd;
    24     result = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &eventItem);
    25 
    26     return result;
    27 }
    28 
    29 void rm_from_epoll(int fd,int epollfd)
    30 {
    31      epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);    
    32 }
    33 
    34 int main(int argc,char **argv)
    35 {
    36     int mEpollFd;
    37     int tmp_fd;
    38     int pollResult;
    39     int i;
    40     int len;
    41     char buf[DATA_MAX_LEN];
    42     
    43     struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
    44 
    45     if(argc < 2){
    46         printf("Usage: %s <file1> [file2] [file3]
    ",argv[0]);
    47         return -1;
    48     }
    49     
    50     
    51     mEpollFd = epoll_create(EPOLL_SIZE_HINT);
    52     if (mEpollFd < 0){
    53         printf("Error epoll_create
    ");
    54         return -1;
    55     }
    56 
    57     for(i = 0;i < argc;i ++){
    58         tmp_fd = open(argv[i],O_RDWR);
    59         add_to_epoll(tmp_fd,mEpollFd);
    60     }
    61 
    62     while(1){
    63         pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, -1);
    64         
    65         for(i = 0 ;i < pollResult;i ++){
    66             printf("Reason: 0x%x
    ",mPendingEventItems[i].events);
    67             len = read(mPendingEventItems[i].data.fd,buf,DATA_MAX_LEN);
    68             buf[len] = '';
    69             printf("get data:%s
    ",buf);
    70         }
    71         sleep(5);
    72     }
    73     
    74 
    75     return 0;
    76 }

    编译文件:gcc -o epoll epoll.c

    3.2实验的结果如下:可实现对文件数据输入监听.

  • 相关阅读:
    Java JMX 监管
    Spring Boot REST(一)核心接口
    JSR 规范目录
    【平衡树】宠物收养所 HNOI 2004
    【树型DP】叶子的颜色 OUROJ 1698
    【匈牙利匹配】无题II HDU2236
    【贪心】Communication System POJ 1018
    【贪心】Moving Tables POJ 1083
    Calling Extraterrestrial Intelligence Again POJ 1411
    【贪心】Allowance POJ 3040
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/8052592.html
Copyright © 2011-2022 走看看