如何使用标准输出选择? [英] How to use select with stdout?

查看:139
本文介绍了如何使用标准输出选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code

    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的能力之前,(第二个printf \\ n); 。但与此code,选择返回一个值!= 1 然后printf的保持unreacheable。它看起来像选择回归的无法的写入标准输出。

I m trying to check the ability to write to the stdout before printf("second printf\n");. but with this code, the select return a value != 1 and then the printf remain unreacheable. it looks like the select return "not possible" to write to the stdout.

你能解释这种现象?

推荐答案

选择()的调用将返回-1,并且errno为22(无效参数),因为你已经在超时了垃圾值。试试这个:

The call to select() is returning -1, and errno is 22 (invalid argument) because you've got junk values in the timeout. Try this:

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

和它应该工作。

这篇关于如何使用标准输出选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆