建立管道从命名管道读取,而不在bash阻塞 [英] Setting up pipelines reading from named pipes without blocking in bash

查看:120
本文介绍了建立管道从命名管道读取,而不在bash阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待与调用打开一个给定的管道文件描述符一个子进程使得open()调用不挂在等待管道的另一端接收的连接。

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.

要证明:

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

在这种情况下, some_program 不运行,直到一些过程有 /tmp/foobar.pipe 打开写;然而, some_program ,即使它不接收命令有一些有用的效果,所以期望的行为是 some_program 来立即执行

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.

机制通过备用脚本语言exec'ing(蟒蛇,perl的,等等)或C封装程序打开 /tmp/foobar.pipe 通过这样做 O_NONBLOCK 标志明显;我在寻找一个纯bash的解决方案,应该是可能的。

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.

推荐答案

打开FD读/设置当管道prevents阻塞写,而不是只读的。

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不需要的阻塞行为,如 5℃;&GT; /tmp/foobar.pipe 在RW模式打开(如只读模式而不是开放与 5℃/tmp/foobar.pipe ),尽​​管 O_NONBLOCK 仍设置。由于瓦尔德纳在IRC://irc.freenode.org/#bash该指针

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.

这篇关于建立管道从命名管道读取,而不在bash阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆