raghulmz raghulmz - 1 year ago 98
Linux Question

use cat to bounce a character between pipes

I have been reading about pipes in linux and I came across this in a book (http://linux-training.be/linuxsys.pdf, page 16)

First they create four pipes with mkfifo

$ mkfifo pipe33a pipe33b pipe42a pipe42b

copy /bin/cat into ./

$ cp /bin/cat proj33 && cp /bin/cat proj42

Then run the following commands

$ echo -n x | ./proj33 - pipe33a > pipe33b
$ ./proj33 <pipe33b >pipe33a &

The book explains the last command as

The commands you see above will create two proj33 processes that use
cat to bounce the x character between pipe33a and pipe33b.

My problem is I do not understand the last command. Especially why '-' is used between ./proj33 and pipe33a. What does '-' do. And how does this set-up act like an infinite loop of exchanging the charatier 'x'?

Answer Source

Explanation: Note for clarity I'm going to call the processes cat1 and cat2 rather than proj33.

  1. echo -n x outputs the character 'x' and feeds it to cat1 which is just a copy of the cat command.
  2. cat1 - pipe33a > pipe33b: First reads stdin (that's what the - means) and writes it to pipe33b. Then cat1 tries to read from pipe33a and it has to wait.
  3. cat2 <pipe33b >pipe33a & Reads the 'x' out of pipe33b and writes it back out to pipe33a
  4. cat1 can now read the 'x' back out of the pipe33a and writes it back out to pipe33b starting the whole process over again.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download