mqueue search

Hi there. I was just wondering if anyone knows a good way to search through a mqueue (message queue)?

What I have is an mqueue called TEMP_Q which messages are placed in, then when a new process is created an mqueue is created for that process, lets call it PROCESS_Q.

Now I want to search through the TEMP_Q to see if any messages were meant for the PROCESS_Q. So I want to do something like this (this is pseudo code):

For (i=0;i<NumberOfItemsInTempQ;i++) Get item from TEMP_Q Is it for the PROCESS_Q? If yes put message in PROCESS_Q If no put it back in TEMP_Q

The problem is because the messages are listed in order of priority i cannot simple take an item out, look at it, return it if its not for the new queue then move on to the next item. The only way that I can see of doing it is to create a second queue say TEMP2_Q, then if the item is not for PROCESS_Q place the message in TEMP2_Q. But this seams like a very inefficant way of doing this as it will require 2 TEMP mqueue, using twice the memory!

Anyone have a better way? Hope my explaination makes sence!!!


I realised the answer shortly after I wrote this post. Simply don’t use the priority (save it as part of the message if u need it for later) this will give all messages the same default priority, effectively making it act like a FIFO.

So you can then take the first item, look at it and put it back if its not for the new process, as its now acting in FIFO mode the returning message appears as a new one, meaning it will be placed at the bottom of the queue.