Turn from: http://blog.csdn.net/haoahua/article/details/2037704
noun explanation:after man Epoll, the following results were obtained:
NAME
EPOLL-I/O Event Notification Facility
Synopsis
#include <sys/epoll.h>
DESCRIPTION
Epoll is a variant of poll (2) the can be used either as Edge or level
triggered interface and scales to large numbers of watched FDS.
Three system calls are provided to set up and control a epoll set:
Epoll_create (2), Epoll_ctl (2), epoll_wait (2).
An epoll set are connected to a file descriptor created by epoll_cre-
Ate (2). Interest for certain file descriptors is then registered via
Epoll_ctl (2). Finally, the actual wait was started by epoll_wait (2).
In fact, all the explanations are superfluous, according to my current understanding, the Epoll model seems to have only one format, so as long as you refer to my code below, you can understand the epoll, the interpretation of the code is already in the comments:
while (TRUE)
{
int Nfds = epoll_wait (m_epoll_fd, m_events, max_events, epoll_time_out);/Waiting for epoll time to occur, equivalent to listening, as for the relevant ports, Need to be bound when initializing the epoll.
if (Nfds <= 0)
Continue
m_bontimechecking = FALSE;
G_curtime = time (NULL);
for (int i=0; i<nfds; i++)
{
Try
{
if (m_events[i].data.fd = = m_listen_http_fd)//If a newly monitored HTTP user is connected to a bound HTTP port, a new connection is established. Because we have a new socket connection, so basically useless.
{
Onaccepthttpepoll ();
}
else if (m_events[i].data.fd = = m_listen_sock_fd)//If a socket user is newly monitored to connect to the bound socket port, a new connection is established.
{
Onacceptsockepoll ();
}
else if (m_events[i].events & Epollin)//If the user is already connected and receives the data, read it.
{
Onreadepoll (i);
}
Onwriteepoll (i);//See if the current active connection has the data that needs to be written out.
}
catch (int)
{
PRINTF ("Catch catch Error/n");
Continue
}
}
m_bontimechecking = TRUE;
OnTimer ()///Do some timed operation, mainly is to delete some short-term users.
}
In fact, the essence of epoll, according to my current understanding, that is, a few short pieces of code, it seems that the times really different, how to accept the problem of a large number of user connections, but now is so easy to fix, really let people have to sigh.
Today I had a day's epoll, want to do a high concurrent agent program. At first really depressed, has been confused, there are several online also introduced epoll articles. But they are not in-depth, do not put some attention to the place to explain. So that a lot of detours, is to share some of their own understanding to everyone, to less detours.
All the functions used by Epoll are declared in the header file sys/epoll.h, where there is no understanding or the function forgets to look.
Epoll compared to select, the biggest difference is:
1epoll return when it is clear to know which Sokcet FD happened, no more than a pair. This improves efficiency.
The fd_setsize of 2select is limited, and epoll is not limit only related to system resources.
1. epoll_create function
function declaration: int epoll_create (int size)
This function generates a Epoll-specific file descriptor. It actually applies a space in the kernel to store what happens and what happens to the socket FD you want to focus on. Size is the maximum number of socket FD You can focus on this epoll FD. It's all right with you. As long as you have room. You can see the difference between the above and the select 2.
22. Epoll_ctl function
function declaration: int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
This function is used to control events on a Epoll file descriptor, you can register events, modify events, and delete events.
Parameters:
EPFD: A epoll-specific file descriptor generated by epoll_create;
OP: Actions to be performed such as registering events, possible values Epoll_ctl_add registration, Epoll_ctl_mod modification, Epoll_ctl_del deletion
FD: The associated file descriptor;
Event: Pointer to Epoll_event;
If the call successfully returns 0, the return is unsuccessful-1
The data structure used
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 * *
};
Such as:
struct epoll_event ev;
Set the file descriptor associated with the event to be processed
EV.DATA.FD=LISTENFD;
Set the type of event to be handled
ev.events=epollin| Epollet;
Registering Epoll Events
Epoll_ctl (Epfd,epoll_ctl_add,listenfd,&ev);
Common Types of events:
Epollin: Indicates that the corresponding file descriptor can be read;
Epollout: Indicates that the corresponding file descriptor can be written;
Epollpri: Indicates that the corresponding file descriptor has an urgent data readable
Epollerr: Indicates that the corresponding file descriptor has an error;
Epollhup: Indicates that the corresponding file descriptor is hung;
Epollet: Indicates the corresponding file description have event occurs;
3. epoll_wait function
function declaration: int epoll_wait (int epfd,struct epoll_event * Events,int maxevents,int Timeout)
This function is used to poll the occurrence of I/O events;
Parameters:
EPFD: A epoll-specific file descriptor generated by epoll_create;
Epoll_event: An array of events for the postback process;
Maxevents: The number of events that can be handled each time;
Timeout: Timeout value waiting for I/O event to occur (I am not quite sure of the unit);-1 is the equivalent of blocking, and 0 is equivalent to non-blocking. General use-1 can
Returns the number of events that occurred.
[Java]View plain copy #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> ; #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <unistd.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h& Gt #include <fcntl.h> #include <sys/epoll.h> #include <sys/time.h> #include <sys/resource.h>[Java]View Plain copy #define MAXBUF 1024 #define MAXEPOLLSIZE 10000[Java]View Plain Copy/* setnonblocking-Set Handle to non-blocking mode/int setnonblocking (int sockfd) {if (Fcntl, SOCKFD, Fcntl (SOCKFD, F_GETFD, 0) | O_nonblock) = = = 1) {return-1; return 0; }[Java] View Plain copy/* handle_message - process message dispatch */ int on each socket handle_message (INT&NBSP;NEW_FD) { char buf[maxbuf + 1]; int len; /* start processing data from each new connection */ bzero (buf, maxbuf + &NBSP;1);