为什么select()总是以EINTR错误号失败
Why does select() keep failing with EINTR errno?
我有一个C++应用程序,它包含以下函数:
int
mySelect(const int fdMaxPlus1,
fd_set *readFDset,
fd_set *writeFDset,
struct timeval *timeout)
{
retry:
const int selectReturn
= ::select(fdMaxPlus1, readFDset, writeFDset, NULL, timeout);
if (selectReturn < 0 && EINTR == errno) {
// Interrupted system call, such as for profiling signal, try again.
goto retry;
}
return selectReturn;
}
通常情况下,这段代码工作得很好,但在一个例子中,我看到它进入了一个无限循环,select()
和EINTR
errno
代码一直在失败。在这种情况下,调用者将超时设置为0秒零微秒,这意味着不要等待并立即返回select()
结果。我认为EINTR
只在发生信号处理程序时发生,为什么我会一次又一次地(超过12个小时)得到信号处理程序?这是Centos 5。一旦我将其放入调试器中查看发生了什么,经过几次迭代后,代码返回时没有EINTR。请注意,要检查的fd是一个套接字。
我可以在上面的代码中添加重试限制,但我想先了解一下发生了什么。
在Linux上,select(2)可以修改timeout参数(通过地址传递)。所以你应该在通话后复印。
retry:
struct timeout timeoutcopy = timeout;
const int selectReturn
= ::select(fdMaxPlus1, readFDset, writeFDset, NULL, &timeoutcopy);
(在您的代码中,经过几次甚至第一次迭代后,timeout
可能为零或非常小)
顺便说一句,我建议使用poll(2)而不是select
(因为poll
对C10K问题更友好)
BTW,EINTR
发生在任何信号上(参见信号(7)),即使没有注册的信号处理程序。
您可以使用strace
来了解程序的整体行为。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 为什么select()总是以EINTR错误号失败