反馈stdin和stdout两个过程 [英] feedback stdin and stdout of two processes

查看:107
本文介绍了反馈stdin和stdout两个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通过stdin和stdout相互连接的进程.

I have two processes that interface with one another over stdin and stdout.

假设我有进程A和进程B.B的标准输出需要输入到A的标准输入中,而A的标准输出需要输入到进程B的标准输入中.

Suppose I have process A and process B. The stdout of B needs to feed into the stdin of A, and the stdout of A needs to feed in to that of process B.

是否有一种简单的方法可以通过简单的命令表达这种关系,或者是否有基本的shell脚本可以启用此关系?

Is there a simple way to express this relationship in a simple command, or is there a basic shell script that can enable this?

提前谢谢.

推荐答案

看看命名管道一个>.为A到B创建一个管道,为B到A创建一个管道.然后启动A,将其stdout重定向到第一个,并将其stdin重定向到第二个.然后从相反的地方开始B.

Take a look at named pipes. Create one pipe for A to B, and one pipe for B to A. Then start A with its stdout redirected to the first, and its stdin redirected to the second. Then start B with the opposite.

它看起来像这样:

mkfifo --mode=0666 /tmp/AtoB
mkfifo --mode=0666 /tmp/BtoA
A < BtoA > AtoB
B < AtoB > BtoA

添加:当然,他们需要某种方式来识别双方都在场.就像一个简单的我在这里,你在吗?"之类的东西.都收到回应.

add: Of course, they'll need some way to recognize that both parties are present. Something like a simple "I am here, are you?" that both receive a response to.

重要:如下面的注释所述,此过程将死锁,两个程序均在读取时被阻止.必须采取某种形式的协调,以确保这种情况不会发生.

Important: As noted in comments below, this process will deadlock with both programs blocking on reads. Some form of coordination will be necessary to ensure this doesn't happen.

这篇关于反馈stdin和stdout两个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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