Linux – Using Linux POSIX IPC message queue

cipclinuxposix

I have to create single Server Process A and multiple client process(es). All should use Linux POSIX IPC message queue for data passing. Message(s) will flow in both direction. It is also possible that at time multiple client process(es) may have registered to Server Process A.

Currently I'm using only one named Message queue which is created and Opened by Server Process A and used/opened (only) by client process(es). This works with two process scenario (i.e One server Process A and one client Process B) but doesn't work with multiple client process(es) and one server process.

The problem i'm facing here is in design/logic. How should I de-multiplex Message at server process A from other client process(es) and also reply from server Process A should sent back to only respective client Process or may be it can sent back to all client process(es) but it must only be processed at respective client process.

For example I'm just giving one scenario.
Suppose Process A has created Message queue X. Process B and C is now coming up and also opening Message queue X. Now Process B sends request message to Message queue X but here the problem is Process A and Process C both will get awake with enqueue event. Here, How process C understand that Message doesn't belongs to it.

  • IPC Message size is small like less than 128 bytes.
  • IPc Message is define structure contains integer and bytes array. No double/float in structure.
  • IPC Message queue is in NON_BLOCKING mode.
  • No mandate on high performance.
  • Language: C
  • Compiler: GCC
  • Platform/OS: Linux
  • IPC Message queue: Only POSIX.
  • I'm not suppose to use other IPC mechansim like System V messages or Unix local socket or Pipes etc.

Please let me know if any more details required.

Please suggest me solution(s) for this problem.

FYI: I have already searched in database but i couldn't find similar question asked/answered already so please make sure before you mark it duplicate. If you find similar problem is already asked and answered then please provide me the link.

Best Answer

Maybe not be the answer you suspect, but consider not using POSIX IPC at all. I designed an application about 15 years ago using SysV IPC. It was one of my worst design decisions.

Today I would use TCP/UDP with a proper protocol instead. Beside the fact that it would allow to move the individual components to different computers in future, the IP stacks are heavily used and supported very well.

With TCP you can establish fine 1:1 and individual 1:many connection oriented communication. With UDP you can make 1:1, 1:many and many:many not-connection oriented communication. You need to keep an eye on security concerns, but there are lots of helpful tutorials and support libraries.

Also for TCP/UDP portability is much better.