简体   繁体   English

单独程序的多对一双向通信

[英]Many-to-one two-way communication of separate programs

I'm trying to make two-way many-to-one communication between programs in Linux. 我正在尝试在Linux中的程序之间进行双向多对一通信。

My plan is the following: One program called "driver" that talks with the hardware needs to communicate with an unknown number of applications in Linux. 我的计划如下:一个名为“驱动程序”的程序与硬件通信需要与Linux中未知数量的应用程序通信。 I read that one of the most common ways for inter process communication is "named pipes". 我读到进程间通信最常见的方法之一是“命名管道”。

The question I haven't found yet is: How new programs should notify the driver that a new program is running so that one more connection (named pipe) between the new program and the driver enstablished? 我还没有发现的问题是:新程序如何通知驱动程序新程序正在运行,以便在新程序和驱动程序之间建立另外一个连接(命名管道)?

All programs will be written in C++ 所有程序都将用C ++编写

在此输入图像描述

In essence, what you've described is a server/client relationship between programs; 实质上,您所描述的是程序之间的服务器/客户端关系; what each program does on either side of the communications bridge is probably irrelevant. 每个程序在通信桥的任何一侧所做的事情可能都是无关紧要的。

Even though these processes appear from the question to be intended to be on the same machine, networking is still available to you via localhost. 即使这些过程出现在要在同一台机器上的问题上,仍然可以通过localhost联网。

If you're not wedded to pipes, why not use a port for the driver (server) known to each program (client), to which the server listens? 如果您没有使用管道,为什么不使用服务器侦听的每个程序(客户端)已知的驱动程序(服务器)端口?

That's pretty much the underlying philosophy of X-Windows, I believe. 我相信,这几乎是X-Windows的基本理念。

Plus, there should be lots of reliable code out there to get you started. 此外,应该有很多可靠的代码可以帮助您入门。

I also think sockets may be a better solution, but if you really want named pipes, I'd do it this way: 我也认为套接字可能是一个更好的解决方案,但如果你真的想要命名管道,我会这样做:

The server opens a pipe named channel_request for reading. 服务器打开一个名为channel_request的管道进行读取。 Any new client opens it for writing and writes a unique ID. 任何新客户端都会打开它进行写入并写入唯一ID。 (PID should work). (PID应该工作)。 The server reads this id and creates a named pipe called channel_[id] . 服务器读取此id并创建名为channel_[id]的命名管道。 The client then opens channel_[id] for reading and can start receiving data. 然后客户端打开channel_[id]进行读取,并开始接收数据。

Note that linux pipes are unidirectional, so if you want two-way communications as shown in your diagram, you will need to open both a channel_[id]_out and a channel_[id]_in . 请注意,linux管道是单向的,因此如果您想要图中所示的双向通信,则需要打开channel_[id]_outchannel_[id]_in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM