Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 259
Jacob Avatar asked Sep 13 '11 12:09

Jacob


People also ask

How do I see message queue in Linux?

There are two ways: Use the Unix command ipcs to get a list of defined message queues, then use the command ipcrm to delete the queue.

What is a Posix message queue?

POSIX message queues are a means by which processes exchange data in the form of messages to accomplish their tasks. They enable processes to synchronise their reads and writes to speed up processes. POSIX message queues are distinct from System V messages.

How do I increase the message queue size in Linux?

According to documentation, /proc/sys/fs/mqueue/msg_max can be used in order to increase the limit of messages in the queue. The documentation also says, that the limit should not exceed HARD_MSGMAX , which is 65,536 since Linux 3.5.

How do I close message queue?

You can remove a message queue using the ipcrm command (see the ipcrm(1) reference page), or by calling msgctl() and passing the IPC_RMID command code. In many cases, a message queue is meant for use within the scope of one program only, and you do not want the queue to persist after the termination of that program.


1 Answers

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.

like image 178
thiton Avatar answered Oct 05 '22 09:10

thiton