我觉得epoll和select最大的区别

最近在用epoll,网速资料很多,大家都说epoll和select的区别比较大,而且select要不停遍历所有的fd,效率要低,而且fd有限制。

但是我认为二者最大的区别在于


先看代码

  while (1)
    {
nfds = epoll_wait(epfd, events, 20, 50000);
cout << nfds <<endl;
if(nfds == -1)
{
perror("epoll_wait");
continue;
}


for(int i =0; i<nfds; i++)
{
if(events[i].events & EPOLLIN)
{
recv_data(events[i].data.fd);
}
}
    }


epoll_wait返回nfds,是有事件的events的个数,在我的应用场景中一般都是0,难道在接下来的for循环中,由于nfds=1 而每次都只看events[0]事件吗,如果假设有20个事件,难道每次只看第一个吗。

所以events数组一直时动态变化的,发生事件的fd排在前面几个,而在select中,fd时固定的,所以需要它去遍历

因此我认为这才是二者最大的区别。

 

posted on 2013-10-16 12:55  我的小人生  阅读(414)  评论(0编辑  收藏  举报