Setting up pipelines reading from named pipes without blocking in bash

前端 未结 2 1977
别跟我提以往
别跟我提以往 2020-12-18 02:17

I\'m looking to call a subprocess with a file descriptor opened to a given pipe such that the open() call does not hang waiting for the other side of the pipe to receive a c

相关标签:
2条回答
  • 2020-12-18 02:48

    Opening the FD read/write rather than read-only when setting up the pipeline prevents blocking.

    To be a bit more specific:

    $ mkfifo /tmp/foobar.pipe
    $ some_program --command-fd=5 5<>/tmp/foobar.pipe
    

    prevents the undesired blocking behavior, as 5<>/tmp/foobar.pipe opens in RW mode (as opposed to opening in read-only mode as with 5</tmp/foobar.pipe) although O_NONBLOCK is still set. Thanks to waldner on irc://irc.freenode.org/#bash for this pointer.

    0 讨论(0)
  • 2020-12-18 02:56

    The only way I know getting this kind of result is a hack:

    mkfifo /tmp/foobar.in
    mkfifo /tmp/foobar.out
    ( cat </tmp/foobar.in ) >/tmp/foobar.out &
    some_program --command-fd=5 5</tmp/foobar.out
    

    perhaps this helps :-)

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