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)
//以上三者可解决相同问题

发布了93 篇原创文章 · 获赞 69 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43705457/article/details/103149049