大部分时间这段代码工作得很好。但是有时当可执行文件运行一段时间后,select()会立即超时,然后进入一个奇怪的状态,它会一直被调用,立即超时。然后它必须从外面被杀死。select()在运行很长时间后立即超时(C++)
我的猜测是,标准输入更改加班的方式是错误的 - 这是选择阻止的方式。
环顾StackOverflow,大多数人的select()问题似乎都可以通过确保每次使用宏重置(FD_ZERO & FD_SET)并使用正确的初始参数进行选择来解决。我不认为这是这里的问题。
int rc = 0;
fd_set fdset;
struct timeval timeout;
// -- clear out the response -- //
readValue = "";
// -- set the timeout -- //
timeout.tv_sec = passedInTimeout; // 5 seconds
timeout.tv_usec = 0;
// -- indicate which file descriptors to select from -- //
FD_ZERO(&fdset);
FD_SET(passedInFileDescriptor, &fdset); //passedInFileDescriptor = 0
// -- perform the selection operation, with timeout -- //
rc = select(1, &fdset, NULL, NULL, &timeout);
if (rc == -1) // -- select failed -- //
{
result = TR_ERROR;
}
else if (rc == 0) // -- select timed out -- //
{
result = TR_TIMEDOUT;
}
else
{
if (FD_ISSET(mFileDescriptor, &fdset))
{
if(rc = readData(readValue) <= 0)
{
result = TR_ERROR;
}
} else {
result = TR_SUCCESS;
}
}
你可以在'rc == 0'情况下添加一些调试代码吗?记录'passedInFileDescriptor'的值,'timeout'和'FD_ISSET(mFileDescriptor,&fdset)'中的字段;如果这个总结的代码是正确的,那么可怕的事情正在发生,并且您可能需要更多的数据来试图将其锁定。 – sarnold 2011-04-01 19:47:47
调用ioctl()和tcsetattr()更改文件描述符的内容。打开文件的公共文件描述符可以在父级和子级之间共享,并且子级可能会更改描述符,然后传播给父级。这假定你不是自己做ioctl()或类似的调用。我正在考虑文件的非阻塞行为。 – 2011-04-01 19:48:50
文件描述符是否已关闭或到达eof? – 2011-04-01 21:27:01