关于epoll_create的size,以及创建出的句柄eploofd是否需要close探讨

size

epoll_creat的函数圆形如下:
int epoll_create(int size);
其中,size的含义值得琢磨。首先参考官方给出的资料:

gun库里的注释

/* Creates an epoll instance.  Returns an fd for the new instance.
   The "size" parameter is a hint specifying the number of file
   descriptors to be associated with the new instance.  The fd
   returned by epoll_create() should be closed with close().  */
extern int epoll_create (int __size) __THROW;

标题linux下man手册

EPOLL_CREATE(2)            Linux Programmer's Manual           EPOLL_CREATE(2)

NAME
       epoll_create, epoll_create1 - open an epoll file descriptor

SYNOPSIS
       #include <sys/epoll.h>

       int epoll_create(int size);
       int epoll_create1(int flags);

DESCRIPTION
       epoll_create() creates a new epoll(7) instance.  Since Linux 2.6.8, the
       size argument is ignored, but must be  greater  than  zero;  see  NOTES
       below.

       epoll_create()  returns  a  file  descriptor referring to the new epoll
       instance.  This file descriptor is used for all the subsequent calls to
       the  epoll  interface.   When  no  longer required, the file descriptor
       returned by epoll_create() should be closed by  using  close(2).   When
       all  file  descriptors referring to an epoll instance have been closed,
       the kernel destroys the instance and releases the associated  resources
       for reuse.

   epoll_create1()
       If  flags  is 0, then, other than the fact that the obsolete size argu‐
       ment is dropped, epoll_create1() is the same  as  epoll_create().   The
       following value can be included in flags to obtain different behavior:

       EPOLL_CLOEXEC
              Set the close-on-exec (FD_CLOEXEC) flag on the new file descrip‐
              tor.  See the description of the O_CLOEXEC flag in  open(2)  for
              reasons why this may be useful.
NOTES
       In  the  initial  epoll_create()  implementation,  the  size   argument
       informed  the  kernel of the number of file descriptors that the caller
       expected to add to the epoll instance.  The kernel used  this  informa‐
       tion  as a hint for the amount of space to initially allocate in inter‐
       nal data structures describing events.  (If necessary, the kernel would
       allocate  more  space  if the caller s usage exceeded the hint given in
       size.)  Nowadays, this hint is no longer required (the  kernel  dynami‐
       cally sizes the required data structures without needing the hint), but
       size must still be greater than zero, in order to ensure backward  com‐
       patibility when new epoll applications are run on older kernels.

其实看man手册就已经明白了,size在Linux2.6.8之后就已经过时了,只需要传入一个>0的值就可以了。

此处指出,网上很多博文都在复制粘贴,以我的阅读经历,这位公众号[全网独家]关于select/poll/epoll的一切为原创,其中关于size的描述:
“epoll_wait参数maxevents告之内核这个events有多大(数组成员的个数),这个maxevents的值不能大于创建epoll_create()时的size,”
是有误的。
例子也很显然,游双的《Linux高性能服务器编程》一书中采用这样的编程风格

#define MAX_EVENT_NUMBER 1024
epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(5);
。。。。
int number = epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1);
。。。。

epollfd是否需要close()

根据man手册里的第二段描述,只要当epoll_create创建的epoll实例里的所有文件描述符都被close()函数关闭了,内核就会自动销毁实例。也就是说,不需要显式调用close去关闭epollfd。
这和epoll.h文件里的注释有不同。注释中特意声明,epoll_create创建的epoll需要由close()调用关闭。

个人认为,man手册合理些,应该是内核在帮我们调用close()。但是应该是加上也无妨。进一步做实验了如果有结论我再来更新。

上一篇:epoll,求知者离我近点


下一篇:从零开始实现TinyWebServer