maxevents parameter in epoll_wait() and the events array size

人盡茶涼 提交于 2020-01-03 06:26:39

问题


In epoll usage, it is usually like the following:

struct epoll_event ev,events[20];
epfd=epoll_create(256);
。。。
nfds=epoll_wait(epfd,events,40,500);

Some articles are saying that the maxevents parameter in epoll_wait (namely the 40 in epoll_wait(epfd,events,40,500);) should not exceed the size parameter in epoll_create(namely the 256).

I think the maxevents parameter should not exceed 20 in ev, events[20], because events can only be registered to 20 events elements; otherwise, if there are 40 sockets which are active, then what will happen?

BTW, if I register more than 20 sockets and there are more than 20 active events(sockets), but the events array events[20] has only 20 events, what will happen?


回答1:


At any single call of epoll_wait you'll at most receive as many events as you have room for, but of course events don't get lost if there are more than that queued up -- you'll simply get them at a later call. Since you'll be calling epoll_wait in a loop anyway, that shouldn't be an issue at all.

The one interesting consideration I can think of is when you have multiple threads read from the same epoll-fd concurrently. In that case the size of your event array determines how many events get handled by a single thread (i.e. a smaller number might give you greater parallelism).



来源:https://stackoverflow.com/questions/16640430/maxevents-parameter-in-epoll-wait-and-the-events-array-size

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