Inter process Communication System (IPC) refers to the set of techniques or methods of sharing data between one or multiple process modules. Each process has its own address space and unique user space and utilizes Linux kernel to allocate space which may be used by the process modules for communication
One such IPC method is the Message Queue. It is a form of asynchronous communication protocol i.e the sender and receiving process need not interact with message queue at the same time. Messages written to queue are stored until the reading process module retrieves them.
Below mentioned are few properties of a message queue
- Each message queue is identified by a unique key / queue ID
- There is a limit to the maximum number of messages stored and is configurable
- There is a limit to the maximum size (bytes) of the queue and is configurable
- It is created under a specific OS user with read / write permissions
Using message queues
Lets say if there are two process modules P1 and P2 which use message queue for IPC to exchange messages. Below is the sequence of events
- A queue name is identified during system design for which a decimal queue key is assigned. In example above queue key decimal value 9002 , equivalent to hex 0x232A is considered.
- In the software design of P1 or P2 , it is coded to create the queue if its not existing.
- When either P1 or P2 process is started , queue gets created by calling linux kernel functions. Queue gets created and is identified by its hexadecimal equivalent key ID i.e 0x232A in our example. P2 switches to queue read mode as per process logic.
- P1 writes a message M1 of say 600 bytes to queue key 9002 . Now there is 1 message pending is queue and total size of queue is 600 bytes
- P2 reads the message M1 from queue key 9002. Once the message is read the queue gets cleared. Pending count – 0 , Total size of queue – 0 bytes
Lets say if P2 process is not started . P1 writes 5 messages of 600 bytes . Pending msg count – 5 , Total size of queue – 3000 bytes. - Once P2 is started , it reads all messages based on entry time . Pending msg count – 0 , Total size of queue – 0 bytes
Key point - It must be noted from above example , a queue gets filled with messages if the process which reads it is not started or in hang state
Linux command ipcs with or without option –q is used to list the message queue details. Example for listing the message queues created in the system
$ ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages
0x000022b4 32769 shareolite 666 0 0
0x00002402 65538 shareolite 666 0 0
0x00002403 98307 shareolite 666 0 0
Where
- “key” indicates the hexadecimal Queue key,
- “msgid” indicates a unique reference number allocated by kernel for the queue which is used along with key by process module to read or write message.
- “owner” is the linux user who created the queue.
- “permissions” indicate the read/write privileges with which a queue is created.
- “used-bytes” indicate the total number of bytes used by messages pending in queue
- “messages” indicate the count of messages pending in queue
Example of a queue with pending messages.
$ ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages
0x000022b4 32769 shareolite 666 0 0
0x00002402 65538 shareolite 666 3600 45
0x00002403 98307 shareolite 666 0 0
Above indicates that there are 45 messages pending , consuming total 3600 bytes equivalent to 80 bytes per message.
To remove / delete a message queue with its message contents , linux command ipcrm with option msg by passing the unique reference “msgid” of the queue as below
$ ipcrm msg 65538
resources deleted
To find process modules using the queue to write or read messages , linux command ipcs is used with option –p by passing the unique reference “msgid” of the queue as below
$ ipcs -p |grep 65538
65538 wdbs 25196 32410
Where 25196 is the PID of process writing to queue and 32410 is the process reading from queue.
Linux IPCS message queues kernel parameters
Below linux kernel parameters decide the message queue characteristics
kernel.msgmax
- Maximum pending messages allowed in queue
- Default value 65536
kernel.msgmni
- Maximum queues allowed for creation
- Default value 16
kernel.msgmnb
- Maximum queue size in bytes
- Default value 1048576 (1 MB)
These parameter values may be modified to suit the application requirement by configuring in /etc/sysctl.conf as below.
kernel.msgmnb = 8388608
kernel.msgmax = 65536
kernel.msgmni = 1000
Post configuration , a server reboot or sysctl reload using below command may be used . Changes will be effective after this step with a fresh login to OS.
$ sysctl -p
Hope this is useful to beginners on understanding basics of IPC message queues , how to manage them