Can't write to named pipe

2020-06-01 08:42发布

问题:

I'm trying to write to a named pipe, made with mkfifo. But when I run the command, (ex) ls > myNamedPipe, I can no longer enter commands into the bash. I can still write characters and that's pretty much it.

回答1:

A named pipe remains opened until you read it from some other place. This is to permit communication between different processes.

Try:

mkfifo fifo
echo "foo" > fifo

Then open another terminal and type:

cat fifo

If you return to you first terminal, you'll notice that you can now enter other commands.

See also what happends with the reverse :

# terminal 1
cat fifo

# terminal 2
echo "foo" > fifo

# and now you can see "foo" on terminal 1

If you want you terminal not to "hang on" when trying to write something to the fifo, attach to the fifo a file descriptor :

mkfifo fifo
exec 3<> fifo
echo "foo" > fifo
echo "bar" > fifo