如何避免命名管道回声收盘FIFO? - Unix的FIFO中的搞怪行为 [英] How to avoid echo closing FIFO named pipes? - Funny behavior of Unix FIFOs

查看:124
本文介绍了如何避免命名管道回声收盘FIFO? - Unix的FIFO中的搞怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要输出一些数据到一个管道,并有其它进程做些什么来通过线数据线。这里是一个玩具的例子:

I want to output some data to a pipe and have the other process do something to the data line by line. Here is a toy example:

mkfifo pipe
cat pipe&
cat >pipe

现在我可以进入我想做的事情,以后pressing进入我立刻看到在同一行。但是,如果以替代第二个管道回声

Now I can enter whatever I want, and after pressing enter I immediately see the same line. But if substitute second pipe with echo:

mkfifo pipe
cat pipe&
echo "some data" >pipe

管道后关闭回声猫管和放大器; 结束,这样我可以不通过管道传送更多的数据。有没有一种方法,以避免关闭管道和接收数据,这样我就可以通过管道从一个bash脚本传递数据的多条线路,并让他们处理,他们到达的过程?

The pipe closes after echo and cat pipe& finishes so that I cannot pass any more data through the pipe. Is there a way to avoid closing the pipe and the process that receives the data, so that I can pass many lines of data through the pipe from a bash script and have them processed as they arrive?

推荐答案

在FIFO被打开阅读,它会阻止调用进程(通常)。当一个进程打开FIFO写入,然后将读卡器畅通。当笔者关闭FIFO,阅读过程中得到EOF(0字节读取),并没有什么更多可以接近除外做到先进先出,然后重新打开。因此,你需要使用一个循环:

When a FIFO is opened for reading, it blocks the calling process (normally). When a process opens the FIFO for writing, then the reader is unblocked. When the writer closes the FIFO, the reading process gets EOF (0 bytes to read), and there is nothing further that can be done except close the FIFO and reopen. Thus, you need to use a loop:

mkfifo pipe
(while cat pipe; do : Nothing; done &)
echo "some data" > pipe
echo "more data" > pipe

另一种方法是将一些流程与FIFO打开。

An alternative is to keep some process with the FIFO open.

mkfifo pipe
sleep 10000 > pipe &
cat pipe &
echo "some data" > pipe
echo "more data" > pipe

这篇关于如何避免命名管道回声收盘FIFO? - Unix的FIFO中的搞怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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