Search

Inter process communication using Message Queues (mq) -1

Message queues are one of the many ways of communicating among processes in linux.

Message queues communicate by sending and reciveing messages, over a common queue. The sender sends the message by writing to the queue and recieves by reading from the queue.

The following are some of the commonly used functions with respect to message queues.

To create or open a message queue we use the function mq_open:

Note: The description of the flags can be found in the man pages. When a new message queue has to be created along with the above two argumetns these two are also mandatory

Retrun:

The function returns a data type mqd_t which is a descrpitor for the message queues. In case of a error it returns -1.

Setting and getting attributes :

The attributes of the message queue are stored in a structure mq_attr.



The fields of these structures can be read by using the function

To change the attributes of an already existing queue we can use the function



Sending and recieving messages :

Once the message queue is opened successfully, the process can communicate with the queue using the queue descriptor along with the functions mq_send to send a message and mq_recieve to recieve a message.

Sending:



Receieving:



Using the above set of functions we can set up a message queue and communicate between processes using them. In the next few posts we will see examples of how to use the above mentioned functions.

Follow by Email