標籤:rom pre sed end ima open net init wait
平台資訊:
核心: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\n", 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\"\n", 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\r\n",event->name);32 } else {33 //closeDeviceByPathLocked(devname);34 printf("delete file: %s\r\n",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> \r\n",argv[0]);53 return -1;54 }55 56 mINotifyFd = inotify_init();57 if (mINotifyFd <= 0){58 printf("Error inotify_init\r\n");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 51214 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]\r\n",argv[0]);47 return -1;48 }49 50 51 mEpollFd = epoll_create(EPOLL_SIZE_HINT);52 if (mEpollFd < 0){53 printf("Error epoll_create\r\n");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\r\n",mPendingEventItems[i].events);67 len = read(mPendingEventItems[i].data.fd,buf,DATA_MAX_LEN);68 buf[len] = ‘\0‘;69 printf("get data:%s\r\n",buf);70 }71 sleep(5);72 }73 74 75 return 0;76 }
編譯檔案:gcc -o epoll epoll.c
3.2實驗的結果如下:可實現對檔案資料輸入監聽.
安卓輸入子系統之inotify與epoll機制【學習筆記】【原創】