epoll_create1与epoll_create区别

引子

第一次看到epoll_create1时非常疑惑 不知道为什么要有这样一个函数 所以在问题解决后写下这篇文章

首先来看一段文档
epoll_create() creates an epoll “instance”, requesting the kernel to
allocate an event backing store dimensioned for size descriptors. The
size is not the maximum size of the backing store but just a hint to
the kernel about how to dimension internal structures. (Nowadays, size
is ignored; 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.
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.

我们可以看到epoll_create的size参数只是一个对内核的建议 现在已经被忽略了 所以这个参数就有一些多余 接下来就出现epoll_create1这个函数 它的参数可以是 ::EPOLL_CLOEXEC 这样就可以在某些情况下解决掉一些问题 即在fock后关闭子进程中无用文件描述符的问题 即fork创建的子进程在子进程中关闭该socket 这篇文章讲的很细

//open O_CLOEXEC
//fcntl FD_CLOEXEC
//epoll_create1(::EPOLL_CLOEXEC)
//以上三者可解决相同问题


事实上epoll_create内部调用epoll_create1,具体可查看这篇博客epoll源码解析(1) epoll_create

原文链接: https://www.cnblogs.com/lizhaolong/p/16437375.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    epoll_create1与epoll_create区别

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/395789

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月5日 下午1:42
下一篇 2023年4月5日 下午1:43

相关推荐