jagadeesh
jagadeesh

Reputation: 111

Single sender and multiple receiver processes using posix message queue in linux

Is there any way such that a writer process after sending a mesage to message queue using mq_send(), multiple reader processes can read the message using mq_receive(). I expect 1 write to mq and 1 read from mq, the message is lost.

So I just want to know if my understanding is wrong. Is there any way so that a single writer and multiple reader processes can communicate using posix message queues.

Upvotes: 11

Views: 8134

Answers (1)

Duck
Duck

Reputation: 27542

Yes your understanding is correct. You can't do this reliably with POSIX message queues. If you want to communicate the same message to different threads/processes reliably you should use a different queue for each reader.

You can do this if you switch to SYSV message queues. Msgsnd() and msgrcv() can manipulate the message type field of a message in some agreed upon protocol. For instance the writer process will make the message type of the message the PID of the reader process; and the reader process will request to read only messages of that message type. Note that this still requires the writer to write a message for each reader process.

Upvotes: 12

Related Questions