ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-11 22:34:03  阅读:232  来源: 互联网

标签:eploofd epoll int 句柄 create file close size


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()。但是应该是加上也无妨。进一步做实验了如果有结论我再来更新。

标签:eploofd,epoll,int,句柄,create,file,close,size
来源: https://blog.csdn.net/cwdben/article/details/116671842

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有