是否有可能在一个进程中保持stdout完整但仍有第二个进程的管道?

时间:2011-11-19 22:47:14

标签: c unix fork pipe file-descriptor

我正在尝试为程序制作记录器。出于这个原因,我正在通过管道并使用 tee 获得第二个子进程,即父进程的 stdout 。贝娄就是一个例子。

if (fork()>0){      //parent

  close(1);
  dup(pip[1]);
  close(pip[0]);

  char a[100]="";
  while (1){
    gets(a);
    puts(a);
  }

} else {           //child

  close(0);
  dup(pip[0]);
  close(pip[1]);

  execlp("tee", "tee", NULL);

}

这很好用。孩子使用管道打印我在父母中键入的任何内容。

但是,可以让父母发送到 stdout ,同时发送到管道吗?

1 个答案:

答案 0 :(得分:1)

查看“write”的手册页,然后你可以打开stdout并写入两个管道:

while(1){
   gets(a)
   write(pip[1], a, len(a))
   puts(a)
}

虽然你真的应该检查你的返回值和所有。

相关问题