Skip to content
Advertisement

How to peek into a Linux (POSIX) message queue without removing an item?

I need to peek into a message queue without removing them. I will go ahead and remove the message queue item only if it complies to certain criteria. How to do this? Following are the APIs I know — but none seems support peeking.

  • mq_close() — close a message queue

  • mq_getattr() — get the current attributes of a message queue

  • mq_notify() — notify the calling process when the queue becomes nonempty

  • mq_open() — open or create a message queue

  • mq_receive() — receive a message from a queue

  • mq_send() — put a message into a message queue

  • mq_setattr() — set the flags for a message queue

  • mq_unlink() — unlink (i.e. delete) a message queue

Is there a way to peek at a message without removing it?

Advertisement

Answer

Peeking is probably a bad idea for a message queue because, like sehe noted, the danger of race conditions. Just assume you have peeked a message; since you cannot lock the queue, you will be unable to reliably retrieve the same message you have peeked. If you have two processes receiving mutually exclusive messages from the same queue, you should be thinking about separating these messages into two queues, for clarity of design and race condition stability.

Answer made short: A peek is very unlikely because it would need explicit locking semantics to carry it out stably.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement