C:stdout挂起linux管道

时间:2012-09-21 14:48:15

标签: c linux bash pipe

我在C中编写了一个简单的I / O回显程序来测试更大的真实程序的问题。这里,linux FD重定向不起作用。

回音程序(又名a.out)是:

#include <stdio.h>

int main(int argc, char **argv) {
  char buff[10];
  while (1) {
    if (fgets(buff, 10, stdin) == NULL) break;
    printf("PRINT: %s \n", buff);
  }
}


从Bash,我运行它:

$ mkfifo IN OUT
$ # this is a method to keep the pipes IN and OUT opened over time
$ while :; do read; echo Read: $REPLY >&2; sleep 1; done <OUT >IN &
$ a.out >OUT <IN &

$ echo xyz >IN

并且没有输出:Bash while循环无法从OUT读取。


让我们将此a.out与cat进行比较,而$ mkfifo IN OUT $ while :; do read; echo Read: $REPLY >&2; sleep 1; done <OUT >IN & $ cat >OUT <IN & $ echo xyz >IN Read: xyz 则按预期方式工作:

cat

最后一行打印在stderr的控制台上。 OUT的输出与a.out不同,可以穿越while并到达Bash {{1}}循环,然后将其打印在控制台上。
a.out有什么问题?

1 个答案:

答案 0 :(得分:6)

尝试在fflush(stdout)之后添加printf(...)

相关问题