2022-03-31

Unix MQ: System V vs. Posix implementations

For inter-process communication, one of the tools is message queue.  Some tutorial can be found in tutorialspoint

I am an old fashion guy and therefore in Unix, I have been using System V MQ for a long time. (see tutorial in systutorials)

But in fact, there is also the other Posix implementation.  You can visit wikipedia for a brief description of the two.  A tutorial for the Posix can be found in systutorials

Recently I have a project to implement MQ for a server and multiple clients and try to study the feasibility of using Posix MQ.  However to my disappointment, the Posix implementation lacks a very useful feature in System V implementation, viz: mtype (aka message type), which  can serve different usages: it can be used to further separate sub-queues for say, different clients, or different message priority, because it has the following logic:

  • If it is 0, then the first message in the queue is read.
  • If it is greater than 0, then the first message in the queue of that type is read
  • If it is less than 0, then the first message in the queue with the lowest type less than or equal to the absolute value will be read.

But in the Posix implementation, only the priority usage remains.

Back to my case, I only need to implement two MQs, one MQ is to send to the server and the other is to send to the clients.

For the server, it can set mtype to 0 and then can read  the messages from all clients.

For the clients, I can set mtype to a positive number (corresponding to the client unique ID) and then each client can use the same queue to read the messages destined to it.

This is great!