Is poll / epoll block enabled? How is this different from asynchronous I / O?

I always had the impression that polling / epoll is not blocking. This is why they are used by non-blocking servers such as Nginx.

But in this question, https://stackoverflow.com/a/166268/ has been indicated several times that polling blocks.

So does the poll / block epoll?

And how is poll / epoll different from async IO?

+4
source share
1 answer

Yes, poll / epoll block. Servers that drop threads to serve clients typically do not scale, and servers that use an I / O event notification model, such as epoll. polling is older and less effective than epoll (O (n) vs O (1)).

[UPDATE]

Nginx is not blocking. When a request arrives, one of the events waiting for epoll_wait is notified and an epoll_wait call is returned. Nginx then goes through the signaling events that serve each of them. Nginx source code is available here ... http://nginx.org/download/nginx-1.1.1.tar.gz

Take a look at the ngx_epoll_process_events function in nginx-1.1.1 \ src \ event \ modules \ ngx_epoll_module.c

[UPDATE2]

See also the man page for epoll_wait (2) ... http://linux.die.net/man/2/epoll_wait

 #include <sys/epoll.h> int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout); 

Specifying a timeout of -1 makes epoll_wait (2) wait indefinitely, when specifying a timeout of zero, returns epoll_wait (2) immediately, even if events are not available (return code is zero).

[Update3]

To make sure the Nginx / epoll blocks, try this on Linux ...

  • Download source and unarchive
  • cd to source directory
  • ./configure --with-debug (NOTE: I had to add libpcre3-dev)
  • make
  • sudo make install
  • To run nginx: /usr/local/nginx/sbin/nginx (NOTE: I had to kill apache sudo /etc/init.d/apache2 stop )
  • sudo gdb
  • file /usr/local/nginx/sbin/nginx
  • b ngx_epoll_module.c:531 (to set a breakpoint)
  • In another terminal window ps -ef | grep nginx ps -ef | grep nginx and use the nginx workflow PID (not the wizard)
  • back to gdb, attach <PID of nginx worker>
  • continue to resume the process

You may need to continue couple of times, but it should eventually block. Then open a browser and go to http: // localhost ... the debugger should crash right after returning epoll_wait .

+8
source

Source: https://habr.com/ru/post/1369219/


All Articles