Q: Where is the bottleneck of the web server? A: IO efficiency. When everyone is working, the system resource caused by the growth of the number of people is being worried, and the SYSTEM EPOLL provided in the Linux 2.6 core provides us with a perfect solution. The efficiency of traditional Select and Poll will result in secondary or three times due to the linear increment of the number of online people, which directly leads to a more significant limit on the number of people supported by the web server. Since Linux provides / dev / epoll devices and subsequent package (SYSTEM EPOLL) for / dev / epoll devices, this phenomenon is greatly eased. If you say a few months ago, everyone is still Epoll is not familiar, then now, the application of EPOLL has been widely popular. So how do you use EPOLL? In fact, it is very simple. By including one header file #include
And a few simple APIs will greatly improve your network server's support.
First create an EPOLL handle through Create_EPOLL (int Maxfds), where maxfds is the maximum number of handles supported by you EPOLL. This function returns a new Epoll handle, and all the operations will be operated by this handle. After running, remember to close this Creuk handle with close ().
Then in your network main loop, each frame calls epoll_wait (int EPFD, INT Timeout) to query all network interfaces, see which one can read, which one can be written. The basic syntax is:
NFDS = EPOLL_WAIT (KDPFD, Events, MaxEvents, -1);
Where KDPFD creates the handle after ePoll_create, Events is a pointer to ePoll_Event *. After the Epoll_Wait is successful, all read and write events will be stored in EPOLL_EVENTS. Max_events is the number of all Socket handles that currently need to listen. The last timeout is the timeout of EPOLL_WAIT. When 0 is 0, it means that it will return immediately. When it is -1, it will wait until there is an event range. When any positive integer, it means such a long time, if there is no event, then range. Generally, if the network main loop is a separate thread, you can use -1, so that some efficiency can be guaranteed, if it is the same thread in the same thread, it can be used to ensure the efficiency of the primary cycle.
After the EPOLL_WAIT range, it should be a loop, all events:
FOR (n = 0; n IF (events [n] .data.fd == listener) {// If it is the primary socket event, it means a new connection into the new connection. Client = Accept (Listener, Struct SockAddr *) & local, & addrlen; IF (Client <0) { PERROR ("accept"); CONTINUE; } SetnonBlocking (client); // placed new connections in non-blocking mode Ev.Events = EPOLLIN | EPOLLET; / / and add new connections to the EPOLL listener. Note that the parameter epollin | EPOLLET does not set the listening to the Socket, if there is a write operation, this time EPOLL does not return the event, if you want to listen to the write, it should be ePollin | Epollout | Epolletev. Data.fd = client; IF (EPOLL_CTL (KDPFD, Epoll_CTL_ADD, Client, & EV) <0) { // After setting the event, add this new Event to the Epoll_CTL to the EPOLL listener, here with ePoll_CTL_ADD to add a new Epoll event, reduce an EPOLL event via EPOLL_CTL_DEL, change an event by ePoll_CTL_MOD to change an event monitoring method . FPRINTF (stderr, "ePoll set insertion error: fd =% d0, Client); Return -1; } } Else // If not the event of a primary socket, the representative is an event of a user socket, and the user socket is handled, such as the read (fd, xxx), or some other processing. DO_USE_FD (events [n] .data.fd); } For the operation of Epoll, the operation of Epoll is as simple as 4 API: Epoll_create, epoll_ctl, epoll_wait and close. If you don't know much about EPOLL, please refer to the article about online games for online games. The world has changed, the problem is worried, it is no problem.