应用场景
类似libaio,属于异步IO 模式,实现批量获取完成的event.
使用方法
step1: 创建epolling fd
可以把epolling fd 想象成一个容器或者代理,里面装需要侦听的文件等描述符。创建polling 描述符对应的API及其接口说明如下:
EPOLL_CREATE(2) Linux Programmer's Manual EPOLL_CREATE(2)
NAME
epoll_create - open an epoll file descriptor
SYNOPSIS
#include <sys/epoll.h>
int epoll_create(int size)
DESCRIPTION
Open an epoll file descriptor by requesting the kernel allocate an event backing store dimensioned for size descriptors. The size is not the
maximum size of the backing store but just a hint to the kernel about how to dimension internal structures. The returned file descriptor
will be used for all the subsequent calls to the epoll interface. The file descriptor returned by epoll_create(2) must be closed by using
close(2).
RETURN VALUE
When successful, epoll_create(2) returns a positive integer identifying the descriptor. When an error occurs, epoll_create(2) returns -1
and errno is set appropriately.
ERRORS
ENOMEM There was insufficient memory to create the kernel object.
epool_control
控制需要把那些文件描述符增加上面创建的容器里,或者控制把那些文件描述符从容器里去掉。
EPOLL_CTL(2) Linux Programmer's Manual EPOLL_CTL(2)
NAME
epoll_ctl - control interface for an epoll descriptor
SYNOPSIS
#include <sys/epoll.h>
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
DESCRIPTION
Control an epoll descriptor, epfd, by requesting the operation op be performed on the target file descriptor, fd. The event 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 */
};
The events member is a bit set composed using 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.
EPOLLPRI
There is urgent data available for read(2) operations.
RETURN VALUE
When successful, epoll_ctl(2) returns zero. When an error occurs, epoll_ctl(2) returns -1 and errno is set appropriately.
epoll_wait
它通过epfd 等待上面指定的event 是否出现:
NAME
epoll_wait - wait for an I/O event on an epoll file descriptor
SYNOPSIS
#include <sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)
DESCRIPTION
Wait for events on the epoll file descriptor epfd for a maximum time of timeout milliseconds. The memory area pointed to by events will con-
tain the events that will be available for the caller. Up to maxevents are returned by epoll_wait(2). The maxevents parameter must be
greater than zero. Specifying a timeout of -1 makes epoll_wait(2) wait indefinitely, while specifying a timeout equal to zero makes
epoll_wait(2) to return immediately even if no events are available ( return code equal to zero ). 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 */
};
The data of each returned structure will contain the same data the user set with a epoll_ctl(2) (EPOLL_CTL_ADD,EPOLL_CTL_MOD) while the
events member will contain the returned event bit field.
RETURN VALUE
When successful, epoll_wait(2) returns the number of file descriptors ready for the requested I/O, or zero if no file descriptor became
ready during the requested timeout milliseconds. When an error occurs, epoll_wait(2) returns -1 and errno is set appropriately.
ERRORS
EBADF epfd is not a valid file descriptor.
EINVAL The supplied file descriptor, epfd, is not an epoll file descriptor, or the maxevents parameter is less than or equal to zero.
EFAULT The memory area pointed to by events is not accessible with write permissions.
应用示例
_epfd = epoll_create(epoll_size);
...
epoll_event evt = { EPOLLOUT, { NULL } };
if (epoll_ctl(_epfd, EPOLL_CTL_ADD,
pipe[1], &evt) < 0) {
COUT << "Fail to add pipe into epfd="
<< _epfd;
return -5;
}
....
epoll_event* e = new (std::nothrow) epoll_event[MAXEVENT];
int k = epoll_wait(_epfd, e, MAXEVENT, 0);
....