How to write messages into a FIFO and read it from another process simultaneously How to write messages into a FIFO and read it from another process simultaneously shell shell

How to write messages into a FIFO and read it from another process simultaneously


From info mkfifo:

Once you have created a FIFO special file in this way, any process can open it for reading or writing, in the same way as an ordinary file. However, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.

So you should open the file for reading in one process (terminal):

cat fifo.file

And open the file for writing in another process (terminal):

echo 'hello' > fifo.file

cat in the sample above stops reading from the file when the end of file(input) occurs. If you want to continue reading from the file, use tail -F command, for instance:

tail -F fifo.file

If you want to write and simultaneously send the strings to another end of the pipe, use cat as follows:

cat > fifo.file

The strings will be sent to another end of the pipe as you type. Press Ctrl-D to stop writing.