2
我有以下代碼如何使用select和stdout?
fd_set set;
struct timeval timeout;
printf("first printf\n"); // displayed
FD_ZERO(&set);
timeout.tv_sec = 1;
FD_SET(fileno(stdout), &set);
if (select(FD_SETSIZE, NULL, &set, NULL, &timeout)!=1)
{
stdout_closed = true;
return;
}
printf("second printf\n"); // Not displayed
餘米嘗試檢查printf("second printf\n");
之前寫入標準輸出的能力。但是使用此代碼,select會返回值!= 1
,然後printf將保持不可刷新狀態。它看起來像選擇返回「不可能」寫入標準輸出。
你能解釋這種行爲嗎?
兩件事:你不清除'timeout.tv_usec';並且'select'的第一個參數應該是最高描述符加上一個(所以'fileno(stdout)+ 1')。還要記住'select'可以返回錯誤的'-1',你需要檢查它。 – 2013-02-20 15:29:43