当前位置 : 主页 > 编程语言 > java >

epoll_create1与epoll_create区别

来源:互联网 收集:自由互联 发布时间:2022-07-04
引子 第一次看到epoll_create1时非常疑惑 不知道为什么要有这样一个函数 所以在问题解决后写下这篇文章 首先来看一段文档 epoll_create() creates an epoll “instance”, requesting the kernel to alloc

引子

第一次看到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​​


网友评论