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.
Both reading and writing are by default blocking. This means that if a process tries to read from a named-pipe that does not have data, it will block. Similarly, if a process write into a named-pipe that has not yet been opened by another process, the writer will block.
Read/Write Are Blocking - when a process reads from a named pipe that has no data in it, the reading process is blocked. It does not receive an end of file (EOF) value, like when reading from a file.
A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be deleted if no longer used. Usually a named pipe appears as a file, and generally processes attach to it for IPC.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With