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;

 

 

 

参考资料

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

posted on 2017-08-20 20:11  qwerhq  阅读(9933)  评论(0编辑  收藏  举报

导航