来自man文档
epoll_create
#include <sys/epoll.h>
int epoll_create(int size);
epoll_create() creates a new epoll(7) instance. Since Linux 2.6.8, the size argument is ignored, but must be greater than zero; see NOTES below.
创建一个epoll实例,在 Linux 2.6.8 以后,size参数被忽略,指定大于0的数
描述
epoll_create() returns a file descriptor referring to the new epoll instance. This file descriptor is used for all the subsequent calls to the epoll interface. When no longer
required, the file descriptor returned by epoll_create() should be closed by using close(2). When all file descriptors referring to an epoll instance have been closed, the kernel
destroys the instance and releases the associated resources for reuse.
引用一个epoll实例,epoll_create() 返回一个文件描述符。epoll底层维护的是一颗红黑树,我们通过epoll_create()返回值来操控这颗红黑树,并增、添、改这颗红黑树的结点
返回值 RETURN VALUE
RETURN VALUE
On success, these system calls return a nonnegative file descriptor. On error, -1 is returned, and errno is set to indicate the error.
返回值:成功返回大于0的文件描述符,失败返回-1,全局变量error被设置成一个合适的值,通过perror打印出错误信息
epoll_ctl
#include <sys/epoll.h>
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
参数:
epfd:是epoll_create() 函数的返回值,获取到这个epoll树的实例,就可以对其进行相关操作。
op:有如下三个取值
Valid values for the op argument are:
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树上添加一个新的待检测的文件描述符,并指定要检测的文件描述符的什么事件,要添加的文件描述符是第三个参数(fd),指定的事件是第四个参数(event)
EPOLL_CTL_MOD
Change the event event associated with the target file descriptor fd.
//修改已经添加到epoll树上的文件描述符事件
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).
//把添加到epoll树上的文件描述符事件删除,最后一个参数event指定为空
fd:(文件描述符)
指定fd,通过第二个参数所设定的选项对其进行操作
event:(epoll event 结构体)
The event argument describes the object linked to the file descriptor fd. The struct epoll_event is defined as:
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event {
uint32_t events; /* Epoll events要检测的事件 */
epoll_data_t data; /* User data variable 联合体,其中有四个成员,因为是联合体,共享内存,所以我们只能用四个中的一个,存储的是用户数据,也就是说,这里面的数据对于内核来说是没有用的。用户用到这个数据是在:把struct epoll_event传递给内核之后,内核检测到event对应的事件触发了,出发了之后会给我们再传出一个struct epoll_event,这个结构体中data信息是我们指定的数据,存储文件描述符的备注信息*/
};
epoll事件指定
The events member is a bit mask composed by ORing together zero or more of the following available event types:
EPOLLIN
The associated file is available for read(2) operations.
检测文件描述符的读事件
EPOLLOUT
The associated file is available for write(2) operations.
检测文件描述符对应的写缓冲区是否可写
EPOLLERR
Error condition happened on the associated file descriptor. This event is also reported for the write end of a pipe when the read end has been closed. epoll_wait(2) will
always report for this event; it is not necessary to set it in events.
异常事件
EPOLLET
Sets the Edge Triggered behavior for the associated file descriptor. The default behavior for epoll is Level Triggered. See epoll(7) for more detailed information about
Edge and Level Triggered event distribution architectures.
epoll有两种模式,使用它可以修改为边沿触发模式
epoll_wait
#include <sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout);
作用:用来检测已经添加到epoll树上的节点已经处于就绪状态,然后对文件描述符作进一步处理。如果epoll树上没有就绪的文件描述符,就会一直堵塞进行检测
参数
epfd:epoll_create的返回值
×event:epoll_event类型结构体的地址,是一个传出参数,传出检测到的已经就绪的文件描述符。即epoll_ctll(int epfd, int op, int fd, struct epoll_event *event)函数中fd参数以及他所附带的结构体存储到epoll_wait()函数的第二个参数中
maxevents:修饰第二个参数,指定结构体的最大容量
timeout:指定阻塞的时间。
若将其指定为-1,epoll_wait()被调用,且在书上没有检测到已经就绪了的节点,这个函数就会一直阻塞检测