inotify和epoll
Posted zhu-g5may
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了inotify和epoll相关的知识,希望对你有一定的参考价值。
参考EventHub.cpp
1、初始化inotify
int mINotifyFd = inotify_init();
2、将要监测的目录添加到inotify
int result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE);
3、读inotify有没有event
#include <string.h> #include <errno.h> #include <unistd.h> #include <sys/inotify.h> #include <stdio.h> int read_process_inotify_fd(int fd) { int res; char event_buf[512]; int event_size; int event_pos = 0; struct inotify_event *event; res = read(fd, event_buf, sizeof(event_buf)); if(res < (int)sizeof(*event)) { if(errno == EINTR) return 0; printf("could not get event, %s ", strerror(errno)); return -1; } while(res >= (int)sizeof(*event)) { event = (struct inotify_event *)(event_buf + event_pos); //printf("%d: %08x "%s" ", event->wd, event->mask, event->len ? event->name : ""); if(event->len) { if(event->mask & IN_CREATE) { printf("create file: %s ", event->name); } else { printf("delete file: %s ", event->name); } } event_size = sizeof(*event) + event->len; res -= event_size; event_pos += event_size; } return 0; } int main(int argc, char **argv) { int ret; if (argc != 2) { printf("Usage: %s <dir> ", argv[0]); return -1; } int mINotifyFd = inotify_init(); int result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE); if (result < 0) { printf("inotify_add_watch error "); return -1; } while (1) { ret = read_process_inotify_fd(mINotifyFd); if (ret) { printf("read_process_inotify_fd error "); return -1; } } return 0; }
以上是关于inotify和epoll的主要内容,如果未能解决你的问题,请参考以下文章