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

查看:28
本文介绍了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.

推荐答案

FIFO 中的 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(), bind(), listen(), accept(), connect(),所有这些都需要与 PF_UNIXAF_UNIXstruct sockaddr_un 一起使用.

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天全站免登陆