Scenarios
Similarly libaio, is an asynchronous IO mode, to achieve volume for the completion of the event.
Instructions
step1: Create a epolling fd
Epolling fd can be thought of as a container or a proxy, which installed needs to listen descriptor files. Creating a descriptor corresponding polling and API interfaces as follows:
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
Those who need to control the file descriptor created above the increase in container or control those file descriptors removed from the container.
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
Whether it is through epfd wait for a specified event occurs above:
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.
Application Examples
_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);
....