3

I have 3 different programs that I would like to intercommunicate with each other.

I have an engine that needs to communicate with 2 bots and the bots with the engine. The engine is written in C++ and the bots can be written in any language. The engine writes output to stdout and both bots need to read the output. Depending on the output from the engine one of the bots will write a response to stdout (it's a turn based game). Here is crude diagram attempting to illustrate what I mean. enter image description here

My current approach is like the following:

mkfifo fifo0 fifo1 fifo2
./engine | tee fifo1 fifo2 < fifo0 &
./bot1 > fifo0 < fifo1 &
./bot2 > fifo0 < fifo2

I read this post on circular I/O which suggests using tail and tee but I am not sure how to make that work with my requirements. Is it possible to do this with pipes? How would this be done with pipes?

3

You've got the < fifo0 in the wrong place. You want it to be engine's stdin, not tee's:

mkfifo fifo0 fifo1 fifo2
< fifo0 ./engine | tee fifo1 fifo2 &
./bot1 > fifo0 < fifo1 &
./bot2 > fifo0 < fifo2

Note that many utilities start to buffer their output when it doesn't go to a tty device (here a pipe (or possibly a socket pair if the shell is ksh93)). On GNU systems and FreeBSD, you may try to use the stdbuf command to disable that buffering:

mkfifo fifo0 fifo1 fifo2
< fifo0 stdbuf -o0 ./engine | tee fifo1 fifo2 &
stdbuf -o0 ./bot1 > fifo0 < fifo1 &
stdbuf -o0 ./bot2 > fifo0 < fifo2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.