Can't write to named pipe

三世轮回 提交于 2019-12-03 17:15:10

问题


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


来源:https://stackoverflow.com/questions/15376562/cant-write-to-named-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!