Thoroughly learn to use epoll (D): Examples ET write analysis

Editor: wind over the summer ChinaUnix blog

First, look at the program four examples.

l program four

  1. #include <unistd.h>
  2. #include <iostream>
  3. #include <sys/epoll.h>
  4. using namespace std;
  5. int main(void)
  6. {
  7.     int epfd,nfds;
  8.     struct epoll_event ev, events [5]; // ev registered for the event, an array of events for the return to be processed
  9.     epfd = epoll_create (1); // need only listen to one descriptor - standard output
  10.     ev.data.fd=STDOUT_FILENO;
  11.     ev.events = EPOLLOUT | EPOLLET; // read status while listening mode settings ET
  12.     epoll_ctl (epfd, EPOLL_CTL_ADD, STDOUT_FILENO, & ev); // register epoll event
  13.     for(;;)
  14.    {
  15.       nfds=epoll_wait(epfd,events,5,-1);
  16.       for(int i=0;i<nfds;i++)
  17.      {
  18.          if(events[i].data.fd==STDOUT_FILENO)
  19.              cout<<"hello world!"<<endl;
  20.      }
  21.    }
  22. };

 

The function of this program is that as long as the standard output write-ready, on output "hello world!".

operation result:

We find that this will be an endless loop. The following detailed analysis about the process of implementation of this program:

(1) First, the initial buffer is empty, there is space to write buffer, then either ET or LT will be added to the corresponding epitem rdlist (corresponding to the first figure of the red line), resulting in the write-ready epoll_wait returns.

(2) program wants standard output "hello world!" And line breaks, because the standard output buffer when the console is the "line buffer", so line breaks lead to empty the contents of the buffer, which corresponds to section II ET the second case write-ready mode - when the old data is sent away, that buffer to be written in fewer content when it will have to trigger a change fd state. So the next write-ready epoll_wait returns. After repeating this process has been the cycle continues.

We look at the program V.

l program five

This is just opposite procedure to remove the four line output of the exchange operation. which is:

 cout<<"hello world!";

Results are as follows:

We see the program into a suspended state. Because the first epoll_wait return after write-ready, the program write "hello world!" To the standard output buffer, but because there is no output newline, so the content of the buffer there has been next epoll_wait time, although there is space to write but under ET mode no longer return to the write-ready. Memories of the first to realize about ET, this is the first reason buffer is empty, leading to epitem join rdlist, ready to return after a removal of this epitem, although after the buffer can still write, but due to the corresponding epitem no longer rdlist in, it will not be ready fd of events in the detection.

l program six

 

  1. int main(void)
  2. {
  3.     int epfd,nfds;
  4.     struct epoll_event ev, events [5]; // ev registered for the event, an array of events for the return to be processed
  5.     epfd = epoll_create (1); // need only listen to one descriptor - standard output
  6.     ev.data.fd=STDOUT_FILENO;
  7.     ev.events = EPOLLOUT; // Use default mode LT
  8.     epoll_ctl (epfd, EPOLL_CTL_ADD, STDOUT_FILENO, & ev); // register epoll event
  9.     for(;;)
  10.    {
  11.      nfds=epoll_wait(epfd,events,5,-1);
  12.      for(int i=0;i<nfds;i++)
  13.     {
  14.       if(events[i].data.fd==STDOUT_FILENO)
  15.          cout<<"hello world!";
  16.     }
  17.    }
  18. };

 

Program is only five six relatively program to modify the default mode ET LT model, we found that the program dead loop again. This time the reason is very clear, because if we write "hello world!" To the buffer, although there is no output buffer emptied, but only under the LT model has a write buffer space is ready to return to write, it will always output "hello world!" when the buffer is full time, the output buffer is automatically cleared brush, it will also cause epoll_wait return to the write-ready.

l program seven

  1. int main(void)
  2. {
  3.     int epfd,nfds;
  4.     struct epoll_event ev, events [5]; // ev registered for the event, an array of events for the return to be processed
  5.     epfd = epoll_create (1); // need only listen to one descriptor - standard output
  6.     ev.data.fd=STDOUT_FILENO;
  7.     ev.events = EPOLLOUT | EPOLLET; // read status while listening mode settings ET
  8.     epoll_ctl (epfd, EPOLL_CTL_ADD, STDOUT_FILENO, & ev); // register epoll event
  9.     for(;;)
  10.    {
  11.      nfds=epoll_wait(epfd,events,5,-1);
  12.      for(int i=0;i<nfds;i++)
  13.     {
  14.        if(events[i].data.fd==STDOUT_FILENO)
  15.            cout<<"hello world!";
  16.        ev.data.fd=STDOUT_FILENO;
  17.        ev.events=EPOLLOUT|EPOLLET;
  18.        epoll_ctl (epfd, EPOLL_CTL_MOD, STDOUT_FILENO, & ev); // re-MOD event (ADD invalid)
  19.    }
  20.  }
  21. };

 

After the seven programs with respect to program five output "hello world!" In each buffer to standard output, re-MOD OUT event. Therefore, in red corresponds to each first pathway described in the ready re-return, causing the program cycle of the output.

Guess you like

Origin www.cnblogs.com/abelchao/p/11703786.html