Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up pipelines reading from named pipes without blocking in bash

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 connection.

To demonstrate:

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

In this case, some_program is not run until some process has /tmp/foobar.pipe open for write; however, some_program has useful effects even when it isn't receiving commands, so desired behavior is for some_program to be immediately executed.

Mechanisms to do this by exec'ing through an alternate scripting language (python, perl, etc) or a C wrapper which open /tmp/foobar.pipe with the O_NONBLOCK flag are obvious; I'm looking for a pure-bash solution, should one be possible.

like image 491
Charles Duffy Avatar asked Oct 07 '08 16:10

Charles Duffy


2 Answers

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.

like image 149
Charles Duffy Avatar answered Oct 19 '22 13:10

Charles Duffy


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 :-)

like image 42
Sec Avatar answered Oct 19 '22 13:10

Sec