Unix上具有多个读取器的命名管道(FIFO) [英] Named Pipes (FIFOs) on Unix with multiple readers

查看:91
本文介绍了Unix上具有多个读取器的命名管道(FIFO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个程序,Writer和Reader.

I have two programs, Writer and Reader.

我有一个从Writer到Reader的FIFO,所以当我在Writer中向stdin写入内容时,它会从Reader打印到stdout.

I have a FIFO from Writer to Reader so when I write something to stdin in Writer, it gets printed out to stdout from Reader.

我尝试在打开两个Reader的情况下执行此操作,并且只有两个Reader程序之一将输出输出到stdout.每次我运行此命令时,Unix选择从哪个Reader程序打印stdout似乎是任意的,但是一旦它选择了其中一个程序,则到stdout的每个输出都会从同一个Reader程序打印.

I tried doing this with TWO Readers open, and I got output to stdout from only one of the two Reader programs. Which Reader program Unix chooses to print stdout from seemed to be arbitrary each time I run this, but once it chooses one of the programs, each output to stdout gets printed from the same Reader program.

有人知道为什么会这样吗?

Does anyone know why this happens?

如果我有两个WRITER程序,它们都可以写入同一管道.

If I have two WRITER programs, they both write to the same pipe okay.

推荐答案

FIF O 中的O表示出".一旦您的数据出",它就消失了. :-)因此,自然而然地,如果另一个进程出现并且其他人已经发出了读取,则数据将不会重复出现两次.

The O in FIFO means "out". Once your data is "out", it's gone. :-) So naturally if another process comes along and someone else has already issued a read, the data isn't going to be there twice.

要完成建议的操作,您应该研究Unix域套接字.联机帮助页此处.您可以编写一个服务器,该服务器可以写入客户端进程,并绑定到文件系统路径.另请参见 socket() listen()

To accomplish what you suggest you should look into Unix domain sockets. Manpage here. You can write a server which can write to client processes, binding to a filesystem path. See also socket(), bind(), listen(), accept(), connect(), all of which you'll want to use with PF_UNIX, AF_UNIX, and struct sockaddr_un.

这篇关于Unix上具有多个读取器的命名管道(FIFO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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