本文介绍了epoll_wait()中的maxevents参数和events数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在epoll用法中,通常类似于以下内容:

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

有些文章说epoll_wait中的maxevents参数(即epoll_wait(epfd,events,40,500);中的40)不应超过epoll_create(即256)中的size参数.

我认为maxevents参数不应超过ev, events[20]中的20,因为事件只能注册到20个事件元素;否则,如果有40个处于活动状态的套接字,那会发生什么?

顺便说一句,如果我注册了20个以上的套接字,并且有20个以上的活动事件(套接字),但是事件数组events[20]只有20个事件,会发生什么?

epoll_wait,所以这根本不是问题.

我可以想到的一个有趣的考虑是,当您有多个线程同时从同一个epoll-fd中读取时.在这种情况下,事件数组的大小决定了单个线程可以处理多少个事件(即,较小的数目可能会为您提供更大的并行度).

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?

解决方案

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

这篇关于epoll_wait()中的maxevents参数和events数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:35