简体   繁体   English

消息队列-多个进程在msgqueue上发送cmd

[英]Message Queue - Multiple processes send cmd on a msgqueue

I have a scenario where multiple processes (p1,p2,etc) simultaneously send a command (post) to a msg Queue(cmd_msg_q). 我有一个场景,其中多个进程(p1,p2等)同时向msg Queue(cmd_msg_q)发送命令(发布)。

And there is one receiver processes (R1) which retrieves the msg from the queue and send appropriate reply back to the processes on another msg queue ( response_msg_q ). 并且有一个接收器进程(R1),它从队列中检索msg并将适当的答复发送回另一个msg队列(response_msg_q)上的进程。

how to reply from the receiver process so that the message is sent to that particular processes ? 如何从接收方进程回复,以便将消息发送到该特定进程?

( Using Posix Message queue. ) Here Process A, B or C gets created dynamically. (使用Posix消息队列。)在这里,进程A,B或C是动态创建的。 Max limit is 20 process which can send commands. 最多可以发送20个进程。

In that case you'll have to integrate some ID of the sender in the message. 在这种情况下,您必须在邮件中集成发件人的某些ID。 Also note that these queues are essentially one-way, so to send messages back you need to create a queue on which the initial receiver will send, and the initial sender will listen for replies. 还要注意,这些队列本质上是单向的,因此要向后发送消息,您需要创建一个队列,初始接收者将在该队列上发送,初始发送者将侦听答复。

This is harder to explain than I initially thought, so let's try with and example. 这比我最初想象的要难解释,所以让我们尝试和示例。 Assume we have programs AB and C where A and B send to C. They do so via the same queue. 假设我们有程序AB和C,其中A和B通过相同的队列发送给C。 C will know who sent the message because the sender's identification is embedded in the message, and will post the reply to either the queue read by B or the queue read by C. C将知道谁发送了该消息,因为发件人的身份已嵌入消息中,并将回复发送到B读取的队列或C读取的队列。

POSIX message queues pretty much require that you establish a second queue for each process and there is a pretty well establish pattern for this. POSIX消息队列非常需要您为每个进程建立第二个队列,并且为此建立了一个很好的模式。

  • Your cmd_msg_q should be an established, well known queue (ie the path name). 您的cmd_msg_q应该是一个已建立的知名队列(即路径名)。 The system can create the queue (eg a startup script) before any applications are launched or the receiving process can create it before any sender processes are launched. 系统可以在启动任何应用程序之前创建队列(例如启动脚本),或者接收进程可以在启动任何发送程序进程之前创建队列。

  • Establish a protocol of message types, minimally: (1) open output queue; 建立消息类型的协议,最少:(1)打开输出队列; (2) process message; (2)处理消息; (3) close output queue. (3)关闭输出队列。 Each message will need to contain an indication of which sender process is submitting the transaction eg pid or some unique identifier. 每个消息将需要包含哪个发送方进程正在提交事务的指示,例如pid或某些唯一标识符。

  • The sender processes create their own queues (eg input to them and output to the receiving "command" process. They send an "open queue" message to the receiver along with the path name of the queue they just created. The command process opens the output queue(s) to the receivers and acks that communication is established. 发送方进程创建自己的队列(例如,输入队列并输出到接收“命令”进程。它们将“打开队列”消息以及刚刚创建的队列的路径名发送给接收方。命令进程将打开队列)。将队列输出到接收器并确认已建立通信。

  • "process" message types are exchanged. 交换“过程”消息类型。 The command process reads input from the combined source queue, and replies to appropriate output queue based on the identifier in each message. 命令过程从组合的源队列中读取输入,并基于每个消息中的标识符来答复适当的输出队列。

  • Client (receiver) queues send a "close queue" message type when they are done. 客户端(接收方)队列完成后将发送“关闭队列”消息类型。 Command process sends an ack and closes that receiver queue. 命令进程发送一个ack并关闭该接收者队列。 Client processes the ack and closes it queue. 客户端处理确认并关闭其队列。

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

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