Can't write to named pipe

后端 未结 1 964
灰色年华
灰色年华 2021-02-07 13:02

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 t

1条回答
  •  一向
    一向 (楼主)
    2021-02-07 13:26

    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
    

    0 讨论(0)
提交回复
热议问题