epoll_ctl函数的使用

#include <sys/epoll.h>

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
做用:
这个系统调用用于操做epoll函数所生成的实例(该实例由epfd指向),向fd实施op操做。
参数一:epfd
由epoll调用产生的文件描述符
参数二:op
操做的类型,具体包含
       EPOLL_CTL_ADD
              Register the target file descriptor fd on the epoll instance
              referred to by the file descriptor epfd and associate the
              event event with the internal file linked to fd.

       EPOLL_CTL_MOD
              Change the event event associated with the target file
              descriptor fd.

       EPOLL_CTL_DEL
              Remove (deregister) the target file descriptor fd from the
              epoll instance referred to by epfd.  The event is ignored and
              can be NULL (but see BUGS below).
参数三:fd
op实施的对象
参数四:event
struct epoll_event {
    __uint32_t events; /* Epoll events */
    epoll_data_t data; /* User data variable */
};

events成员变量:
能够是如下几个宏的集合: EPOLLIN :表示对应的文件描述符能够读(包括对端SOCKET正常关闭); EPOLLOUT:表示对应的文件描述符能够写; EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来); EPOLLERR:表示对应的文件描述符发生错误; EPOLLHUP:表示对应的文件描述符被挂断; EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来讲的。 EPOLLONESHOT:只监听一次事件,当监听完此次事件以后,若是还须要继续监听这个socket的话,须要再次把这个socket加入到EPOLL队列里。

data成员变量:
是一个union类型的变量,类型定义以下
typedef union epoll_data {
    void *ptr;
    int fd;
    __uint32_t u32;
    __uint64_t u64;
} epoll_data_t;

 

 

 

参考资料html

http://www.man7.org/linux/man-pages/man7/epoll.7.htmllinux

相关文章
相关标签/搜索