c - select() 超时后不返回

标签 c linux select fifo

我创建了一个名为“readmsg”的可执行文件。它的源代码如下。 select()如果我只执行 readmsg 就可以工作在 shell 中(我可以看到 timeout 的输出)。

但是如果我通过命令创建一个 FIFO 文件:mknod /tmp/message p , 并执行 readmsg < /tmp/message在外壳中。结果,select()如果我不在/tmp/message中写点什么就不能返回.我的问题是:为什么我无法获得 timeout输出?

“readmsg”源码:

    #define STDIN 0
    fd_set fds;
    struct timeval tv;
    while (1) {
        FD_ZERO(&fds);
        FD_SET(STDIN, &fds);
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        ret = select(STDIN + 1, &fds, NULL, NULL, &tv);
        if (ret > 0) {
            printf("works\n");
            if (FD_ISSET(STDIN, &fds)) {
                // read ...
            }
        } else if (ret == 0) {
            printf("timeout!!\n");
        } else {
            printf("interrupt\n");
        }
    }

最佳答案

谢谢@Mat。添加后 printf()靠近main() ,也没有输出。即使没有readmsg的进程ID什么时候执行 ps . 于是证明了readmsg < /tmp/message的过程在 FIFO 准备好写入之前被阻塞。

没有任何错误。事实上,readmsg从重定向的 FIFO 文件中读取消息时效果很好。

关于c - select() 超时后不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302579/

相关文章:

objective-c - xcode 中的 strcpy

c - *** 错误 `./a.out' : realloc(): invalid next size: 0x00000000009c4010 ***

c - 疯狂: not understood

MySQL SELECT 从另一个表的数据开始?

oracle - 计算 Oracle 中的表数

c++ - 使用 C 在 linux 中设置用户的补充组 ID

将 double 转换为 int 不准确(1 关)

linux - 从 bash 脚本启动进程时的不同 pid

linux - 当进程在计算机集群上运行时如何访问标准输出?

mysql - SQL 查询计数选择