linux打印另一个进程,将两个进程的stdout重定向到Linux C中的另一个进程的stdin

我遇到了关于多进程重定向stdout的问题.

假设我有进程A,我在A中使用fork(),然后我得到进程A和B.我在B中使用fork(),最后我得到进程A,B和C.B和C都在实现其他程序EXEC().

现在,我尝试用两个管道将A和B的stdout重定向到C的stdin.

#include

#include

#include

int main()

{

int AtoC [2];

pipe(AtoC);

int fd1,fd2;

fd1=fork();

if(fd1>0)

{

/***In process A, I do the following steps: ***/

close(AtoC[0]);

dup2(AtoC[1], STDOUT_FILENO);

/* program running in process A */

}

else

{

int BtoC [2];

pipe(BtoC);

fd2=fork();

if(fd2>0)

{

/***In process B, I do the following steps: ***/

close(AtoC[1]);

close(BtoC[0]);

dup2(BtoC[1], STDOUT_FILENO);

/*** execute another program in process B using execl(); ***/

}

else

{

/*** In process C, I do the following steps: ***/

close(AtoC[1]);

close(BtoC[1]);

dup2(AtoC[0],STDIN_FILENO);

dup2(BtoC[0],STDIN_FILENO);

/*** execute another different program in process C using execl(); ***/

}

}

}

现在,在这两个陈述之后:

dup2(AtoC[0],STDIN_FILENO);

dup2(BtoC[0],STDIN_FILENO);

进程C的stdin最终重定向到BtoC [0],这是进程B的stdout.进程A的stdout没有传递给进程C的stdin.

我的问题是,是否有任何解决方案可以让我同时将进程A和B的stdout重定向到进程C的stdin.

另一个问题是,如果我还想在屏幕上打印进程A的标准输出,我该怎么办?我在命令行中知道命令tee.我尝试在进程A中使用相应的函数tee(int fd_in,int fd_out,size_t len,unsigned int flags),但是我无法打印任何进程A的stdout.

任何建议表示赞赏,谢谢.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值