Feedback stdin and stdout of two processes

I have two processes that interact with each other via stdin and stdout.

Suppose I have process A and process B. The default value of B should be passed to stdin A, and stdout A should be connected to the file of process B.

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

Thanks in advance.

+6
source share
3 answers

See named pipes . Create one channel for AB and one channel for B in A. Then start A, and its stdout is redirected to the first, and its stdin is redirected to the second. Then run B with the opposite.

It will look something like this:

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

add: Of course, they will need some way to admit that both sides are present. Something like the simple "I'm here, right?" that both get an answer.

Important: As noted in the comments below, this process will be inhibited with the blocking of both programs when reading. Some form of coordination will be necessary to prevent this from happening.

+2
source

Bash 4 introduces coproc :

 declare -a FDS coproc FDS { process_A; } process_B <&${FDS[0]} >&${FDS[1]} 
+5
source

(I would comment on Keith's answer, but there is still not enough reputation.)

Testing this on OpenBSD, I could not run the scripts by doing:

 ./a < btoa > atob & ./b < atob > btoa 

( atob and btoa are FIFO, and the scripts a and b duplicate stdin)

However, after I also based on the second one, as soon as I ran > btoa in my shell (the null command, however, opening btoa for writing), they started. (Beware of the infinite loop!) I guess that means you need a third process.

I am not sure if the FIFO behavior is standardized in such cases (for example, opening multiple processes for writing).

0
source

Source: https://habr.com/ru/post/910619/


All Articles