Does epoll(), do its job in O(1)?

后端 未结 1 1120
花落未央
花落未央 2021-01-31 07:06

Wikipedia says

unlike the older system calls, which operate at O(n), epoll operates in O(1) [2]).

http://en.wikipedia.org/wiki/

1条回答
  •  一生所求
    2021-01-31 08:01

    This makes sense once you look for ep_find. I only spent a few minutes with it and I see ep_find is only called by epoll_ctl.

    So indeed, when you add the descriptors (EPOLL_CTL_ADD) that costly operation is performed. BUT when doing the real work (epoll_wait) it isn't. You only add the descriptors in the beginning.

    In conclusion, it's not enough to ask the complexity of epoll, since there is no epoll system call. You want the individual complexities of epoll_ctl, epoll_wait etc.

    Other stuff

    There are other reasons to avoid select and use epoll. When using select, you don't know how many descriptors need attention. So you must keep track of the biggest and loop to it.

    rc = select(...);
    /* check rc */
    for (s = 0; s <= maxfd; s++) {
        if (FD_ISSET(s)) {
            /* ... */
        }
    }
    

    Now with epoll it's a lot cleaner:

    nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
    /* check nfds */
    for (n = 0; n < nfds; ++n) {
        /* events[n].data.fd needs attention */
    }
    

    0 讨论(0)
提交回复
热议问题