ほとんどの場合、このコードは問題なく動作します。しかし、実行可能ファイルがしばらく実行されていると、select() がすぐにタイムアウトしたように見え、その後、呼び出され続けるという奇妙な状態になり、すぐにタイムアウトを何度も繰り返すことがあります。それからそれは外側から殺されなければなりません。
私の推測では、標準入力が時間の経過とともに変化する方法に問題があると思います-それがselectがブロックしているものです。
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;
}
}