INotify and epoll mechanism of Android input subsystem "Learning notes" "Original"

Source: Internet
Author: User
Tags epoll inotify

Platform Information:
Kernel: linux3.1.0
System: android5.0
Platform: tiny4412

Zhung Zebin (Welcome reprint, please specify the author)

Description: Teacher Wei's android video learning notes

First, in the Android input subsystem how to listen to file generation and listen to the file whether there is data input, the file is mainly used to monitor the inotify mechanism to listen to the creation and deletion of files. Use Epoll can be used to listen to the file whether there is data changes. Here are the two mechanisms for programming, a simple understanding and how to use.

Second, the use of inotify monitoring file creation and deletion.

2.1 Let's take a look at the phenomenon and see how the specific code is implemented.

INotify that run in the background can detect the creation and deletion of files.

2.2 Implementation of the Code

1#include <unistd.h>2#include <stdio.h>3#include <sys/inotify.h>4#include <string.h>5#include <errno.h>6 7 8 intREAD_PROCESS_INOTIFY_FD (intFD)9 {Ten     Charevent_buf[ +]; One     intevent_size; A     intEvent_pos =0; -     intRes; -     structInotify_event *Event; the      -  -res = read (FD, EVENT_BUF,sizeof(EVENT_BUF)); -     if(Res < (int)sizeof(*Event)) { +         if(errno = =eintr) -             return 0; +printf"could not get event,%s\n", Strerror (errno)); A         return-1; at     } -  -      while(Res >= (int)sizeof(*Event)) { -         Event= (structInotify_event *) (Event_buf +event_pos); -         //printf ("%d:%08x \"%s\ "\ n", Event->wd, Event->mask, Event->len? Event->name: ""); -         if(Event-Len) { in             if(Event->mask &in_create) { -                 //opendevicelocked (devname); toprintf"Create file:%s\r\n",Event-name); +}Else { -                 //closedevicebypathlocked (devname); theprintf"Delete file:%s\r\n",Event-name); *             } $         }Panax NotoginsengEvent_size =sizeof(*Event) +Event-Len; -Res-=event_size; theEvent_pos + =event_size; +     } A  the     return 0; +  - } $  $ intMainintargcChar**argv) - { -     intMINOTIFYFD; the     intresult; - Wuyi     if(ARGC! =2){ theprintf"Usage:%s <dir> \ r \ n", argv[0]); -         return-1; Wu     } -  AboutMINOTIFYFD =inotify_init (); $     if(MINOTIFYFD <=0){ -printf"Error inotify_init\r\n"); -         return-1; -     } A  +      theresult = Inotify_add_watch (MINOTIFYFD, argv[1], In_delete |in_create); -  $      while(1){ the read_process_inotify_fd (MINOTIFYFD); the     } the      the      -  in     return 0; the}

Compile command: Gcc-o inotify inotify.c, then follow the 2.1 steps to run.

Third, use Epoll to listen to the file whether there is data write.

The specific implementation of the 3.1 code is as follows:

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 Ten Static Const intEpoll_size_hint =8; One Static Const intEpoll_max_events = -; A  - #defineData_max_len 512 -  the  - intAdd_to_epoll (intFdintEPOLLFD) - { -     intresult; +     structepoll_event Eventitem; -      +memset (&eventitem,0,sizeof(Eventitem)); AEventitem.events =Epollin; atEVENTITEM.DATA.FD =FD; -result = Epoll_ctl (EPOLLFD, Epoll_ctl_add, FD, &eventitem); -  -     returnresult; - } -  in voidRm_from_epoll (intFdintEPOLLFD) - { to Epoll_ctl (EPOLLFD, Epoll_ctl_del, FD, NULL);  + } -  the intMainintargcChar**argv) * { $     intMEPOLLFD;Panax Notoginseng     inttmp_fd; -     intPollresult; the     inti; +     intLen; A     CharBuf[data_max_len]; the      +     structepoll_event mpendingeventitems[epoll_max_events]; -  $     if(ARGC <2){ $printf"Usage:%s <file1> [file2] [file3]\r\n", argv[0]); -         return-1; -     } the      -     WuyiMEPOLLFD =epoll_create (epoll_size_hint); the     if(MEPOLLFD <0){ -printf"Error epoll_create\r\n"); Wu         return-1; -     } About  $      for(i =0; i < Argc;i + +){ -TMP_FD =open (ARGV[I],O_RDWR); - Add_to_epoll (TMP_FD,MEPOLLFD); -     } A  +      while(1){ thePollresult = epoll_wait (MEPOLLFD, Mpendingeventitems, Epoll_max_events,-1); -          $          for(i =0; i < Pollresult;i + +){ theprintf"reason:0x%x\r\n", mpendingeventitems[i].events); theLen =read (Mpendingeventitems[i].data.fd,buf,data_max_len); theBuf[len] =' /'; theprintf"Get data:%s\r\n", buf); -         } inSleep5); the     } the      About  the     return 0; the}

Compiled files: gcc-o epoll epoll.c

The results of the 3.2 experiment are as follows: can be used to monitor file data input.

INotify and epoll mechanism of Android input subsystem "Learning notes" "Original"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.