Reputation: 12371
In Unix system, I just knew that we could use FIFO file for communication between two processes and I've tested it with C projects.
Now I'm wondering if we can do something like this:
I've tried the following, but it doesn't work. On one terminal:
mkfifo fifo.file
echo "hello world" > fifo.file
On the other terminal:
cat fifo.file
Now I can see the "hello world"
. However, both processes finish immediately and I can't continue typing / reading the fifo.file
anymore.
Upvotes: 3
Views: 11471
Reputation: 21492
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.
Upvotes: 3