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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南