epoll是在内核2.6引入的方法 #include int epoll_create(int numDescriptors); //创建epoll文件描述符 //numDescriptors创建多少个文件描述符
int epoll_ctl(int epfd, int op, int fd, struct epoll_event * event) op:EPOLL_CTL_ADD 把event添加到文件描述符集epfd中 EPOLL_CTL_DEL 把event从文件描述符集epfd中删除掉 EPOLL_CTL_MOD 对epfd中的event中的信息更新
int epoll_wait(int epfd, struct epoll_event * event, int maxevents, int timeout) //堵塞直到有一个或多个数据可读或可写 //event和maxevents指定了缓冲区的结构 返回缓冲区里分配了多少个event结构 //当超时函数返回0,
struct epoll_event { int events; //指定哪种事件将被监控 union { void * ptr; int fd; unsigned int u32; unsigned long u64; }data; //只要有相应的事件发生,date就返回相应的程序,知道哪个文件描述符需要处理 };