简体   繁体   中英

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).

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 ).

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. Max limit is 20 process which can send commands.

In that case you'll have to integrate some ID of the sender in the message. 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. 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.

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.

  • Your cmd_msg_q should be an established, well known queue (ie the path name). 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; (2) process message; (3) close output queue. Each message will need to contain an indication of which sender process is submitting the transaction eg pid or some unique identifier.

  • 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. Client processes the ack and closes it queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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