Thoroughly learn to use Epoll (v) Considerations in--et Mode

Source: Internet
Author: User
Tags epoll readable
thoroughly learn Epoll (v) Considerations in--et mode--lvyilong316 5.1 et mode read and write

In the previous sections of the analysis, we can know that when the epoll work in the ET mode, for the read operation, if read does not read the data in buffer, then the next time will not be read-ready notification, resulting in the data already in the buffer no chance to read out, Unless there is new data to arrive again . For write operations, the main reason is that FD is usually non-blocking caused by the ET mode-- how to ensure that the data written by the user is written out.

To solve the read and write problems in the above two et modes, we must implement the following:

A. for reading, read as long as there is data in buffer;

B. for write, as long as the buffer has space and the user requested to write the data has not been finished, has been written.

To achieve the above, A and B two effects, we have two ways to solve.

L Method One

(1) After each read-in operation (READ,RECV), the user actively epoll_mod in event, at this time as long as the FD buffer and data can be read, then epoll_wait will return to read ready.

(2) After each output operation (write,send), the user actively epoll_mod out event, at this time as long as the buffer of the FD can send data (send buffer dissatisfaction), then epoll_wait will return to write ready (sometimes using the mechanism to notify Epoll_ Wai woke up).

The principle of this method we discussed before: when there is data readable in buffer (that is, buffer is not empty) and the user to the corresponding FD Epoll_mod in event when the et mode returns to read Ready, when there is a writable space in buffer (that is, buffer is dissatisfied) and the user Epoll_mod out Events write-ready when returned.

So get the following solution:

if (Events[i].events&epollin)//If the data is received, then the read-in

{

cout << "Epollin" << Endl;

SOCKFD = EVENTS[I].DATA.FD;

if ((n = read (SOCKFD, line, MAXLINE)) >0)

{

Line[n] = '/0 ';

cout << "read" << line << Endl;

if (n==maxline)

{

EV.DATA.FD=SOCKFD;

ev.events=epollin| Epollet;

Epoll_ctl (Epfd,epoll_ctl_mod,sockfd,&ev); Data is not finished, re-mod in event

}

Else

{

EV.DATA.FD=SOCKFD;

ev.events=epollin| Epollet;

Epoll_ctl (Epfd,epoll_ctl_mod,sockfd,&ev); Data in buffer has been read, mod out event

}

}

else if (n = = 0)

{

Close (SOCKFD);

}

}

else if (events[i].events&epollout)//If there is data sent

{

SOCKFD = EVENTS[I].DATA.FD;

Write (sockfd, line, N);

EV.DATA.FD=SOCKFD; Set the file descriptor for the read operation

ev.events=epollin| Epollet; To set the read operation event for the injection test

Epoll_ctl (Epfd,epoll_ctl_mod,sockfd,&ev); Modify the event to be handled on SOCKFD as Epolin

}

Note: For write operations, because SOCKFD is working in blocking mode, there is no need for special handling, as is the case with Lt.

Analysis: There are several problems with this approach:

(1) For the read operation after the judgment-if (n==maxline), can not be explained in this case buffer must have read the data, imagine if there is a total of buffer MAXLINE byte data. In this way, the MoD in will no longer be notified, and there will be no chance to sockfd mod out.

(2) If the service side in other ways can be in the appropriate time to the corresponding SOCKFD MOD out, whether this method is preferable. We first think about why to use the ET mode, because the ET mode can reduce epoll_wait and other system calls, and we here every read after the MoD in, and then epoll_wait, is bound to reduce efficiency, this is not counterproductive.

In conclusion, this method should not be used.

L Method Two

read: Reads as long as it is readable, until it returns 0, or errno = Eagain

Write : As long as it is writable, write until the data is sent, or errno = Eagain

if (Events[i].events & Epollin)

{

n = 0;

While ((nread = Read (FD, BUF + N, BUFSIZ-1)) > 0)

  {

n + = nread;

      }

if (nread = =-1 && errno! = eagain)

{

Perror ("read error");

}

EV.DATA.FD = FD;

ev.events = Events[i].events | Epollout;

Epoll_ctl (EPFD, Epoll_ctl_mod, FD, &ev);

}

if (Events[i].events & Epollout)

{

int nwrite, data_size = strlen (BUF);

n = data_size;

while (n > 0)

{

nwrite = Write (fd, buf + data_size-n, n);

if (Nwrite < n)

  {

if (nwrite = =-1 && errno! = eagain)

 {

perror (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.