1

我有以下代码

    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");。但是使用此代码,选择返回一个值!= 1,然后 printf 仍然无法访问。看起来选择返回“不可能”写入标准输出。

你能解释一下这种行为吗?

4

1 回答 1

4

对 select() 的调用返回 -1,而 errno 为 22(无效参数),因为您在超时中有垃圾值。试试这个:

FD_ZERO(&set);
timeout.tv_sec = 1;
timeout.tv_usec = 0; /* ADD THIS LINE to initialize tv_usec to 0 so it's valid */

它应该可以工作。

于 2013-02-20T15:36:42.053 回答