maxevents parameter in epoll_wait() and the events

2020-08-09 06:43发布

问题:

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).