In the previous post we saw how we can create a message queue and use the same to communicate between processes.
The message queues also have the ability to assign priorities to messages that are sent, so that while recieving the messages with higher priority are recieved first even if they are sent after a low priority message.
Let us look at this operation of the message queues using an example.
We will use the example similar to the one used in the previous post, only difference being the queue is being created by the sender where as in the previous example it was
created by the reciever.
To set the priority of a message we pass the priority as an integer number to the call
mq_send. For eg :
The last argument "1" is the priority of the message, higher the integer number higher the priority.
Let us send 4 messages each with a different priority and send the least prioirty message first. i.e.
And in the recieve program we will recieve using mq_recieve 4 times, and while recieveing we will read the priority of the message and display the same.
The complete codes for the two programs will be as below.
Compile the two programs
First execute the send program, so that we send messages of four different priorities in the queue
Now to read the message execute the receive program
The output should be :
Thus we can see that the message queue does not perform as a fifo and the message with higher priority is read before message with lower priority .Messages of the same prioirty are read in the fifo manner.