开发者

How to get a unique POSIX message queue?

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 wit开发者_Go百科h 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 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

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 mkstemp and tmpfile do under the hood. Borrow any of the "tmp"/"temp" name selection algorithms to generate something "/reasonably_unique", mq_open it O_CREAT|O_EXCL, and then mq_unlink it. 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_open and the mq_unlink? Yes.

Alternative implementation

Alternatively, use a SOCK_DGRAM socketpair or pipe instead. Those offer similar semantics to POSIX message queues — attr.mq_msgsize becomes SO_SNDBUF/SO_RCVBUF or an agreement to respect PIPE_BUF, mq_notify becomes I/O selectability (probably already the case) — though you do lose message prioritization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜