I’m working on a simple parallel application in which I want to use a single process to maintain status information about a family of worker processes. It seems relatively easy to set up a POSIX message queue in which all of the worker bees can send periodic updates to the status maintainer. My problem? A POSIX message queue has to have a name. I don’t want to pick a name; all I care about is getting a unique message queue, much as I would using SYSV message queues with IPC_PRIVATE. For a unique filename I could use mkstemp(3) or for a unique open file descriptor I could use tmpfile(3). How should I get a unique POSIX message queue?
I’m working on a simple parallel application in which I want to use a
Share
Well, with POSIX message queues, you do have to specify a name, but you don’t have to preserve it nor allow others to use the same queue by that name.
IPC_PRIVATE mimicry
Do what
mkstempandtmpfiledo under the hood. Borrow any of the “tmp”/”temp” name selection algorithms to generate something “/reasonably_unique”,mq_openit O_CREAT|O_EXCL, and thenmq_unlinkit. Child worker processes can then inherit the message queue descriptor.Caveat: Could root or your EUID gone rogue figure out what you’re doing and jump in between the
mq_openand themq_unlink? Yes.Alternative implementation
Alternatively, use a SOCK_DGRAM
socketpairorpipeinstead. Those offer similar semantics to POSIX message queues —attr.mq_msgsizebecomes SO_SNDBUF/SO_RCVBUF or an agreement to respect PIPE_BUF,mq_notifybecomes I/O selectability (probably already the case) — though you do lose message prioritization.