When to use the POLLOUT event of the poll C function?

老子叫甜甜 提交于 2019-12-20 10:13:09

问题


I wrote a small TCP servers with socket() + POLLIN poll() + recv() + send(), but I don't know when to use POLLOUT poll or select writefds to poll on writable event.

Can anyone give me an example of the real usage of POLLOUT?


回答1:


The usual pattern is to use non-blocking file descriptors with poll() like this:

  • When getting ready to poll(),
    • Always set POLLIN because you are always interested in reading what the other end of the socket has send you.
      • Except if you have a large backlog of incoming data and you intentionally want to make the other end wait before sending more.
    • Set POLLOUT only if you have outstanding data to send to the other end.
  • Upon return from poll(), if it indicates that data is available to read,
    • read it and do something with it
  • Upon return from poll(), if it indicates that the socket is writable,
    • Try sending your outstanding data.
      • If you managed to write all of it, you're not going to set POLLOUT next time through the loop
      • If you only managed to send some of it (or none of it) then keep the rest for later. You will set POLLOUT the next time through the loop.
  • When you have new data to send (either in response to data you read or in response to some external event), you have two choices:
    • Eagerly try to send some of it right away. You may successfully send none, some, or all of it. Just like with the previous case, keep the portion of the data that wasn't written for next time and plan to set POLLOUT the next time through the loop only if there was some data left.
    • Just keep a hold on the data and plan to set POLLOUT the next time through the loop. (This choice is often easier to program because you only need to handle writing data in one place in your loop but on the other hand it delays writing the data until the next time through the loop.)



回答2:


From nginx source, I found that:

If there is some data to send out, nginx tries to send it with a syscall (maybe writev). However, if nginx can not send total data at one time, it will set POLLOUT on pollfd, if using poll event, to wait for a writable event. When getting a writable event, nginx will send the left data.

It is easy to reproduce this case when nginx tries to response large static file




回答3:


Working on a Raspberry PI 3, Debian, using c++ 98 with gcc . . .

In an implementation of the Acceptor / Connector pattern and Reactor / Proactor / ACT pattern I regulary use POLLOUT in the following sequence:

  1. Open a socket using the socket function.
  2. Set the socket file descriptor to non blocking mode with fcntl.
  3. Call connect and check return code.

In most cases connect returns a -1. Because of the non blocking file descriptor this is very likely. Then I check the result code.

If it is EINPROGRESS, I register an event handler in the reactor (which uses ppoll or epoll) with POLLOUT. When the connection is finally done, poll returns with POLLOUT set.

Then I create a new TcpConnection class and communicate.



来源:https://stackoverflow.com/questions/12170037/when-to-use-the-pollout-event-of-the-poll-c-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!