Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Properties of message queues
3.
System call used for IPC using message queues
4.
Code Implementation
5.
Frequently Asked Questions
5.1.
What are the system calls used in the message queue?
5.2.
What is a message queue?
5.3.
Which function returns an identifier to the message queue?
5.4.
Which function is used to write the message onto the message queue?
5.5.
Which function is used to read the message from the message queue?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

IPC using Message Queues

Author ANKIT KUMAR
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Operating Systems

Introduction

In this article, we will learn how message queues are used for inter-process communication. This article assumes that the reader has a basic understanding of inter-process communication and the shared memory method.

The message queues can be thought of as a linked list of messages that is used by the communicating processes. The message queues are stored within the kernel. Since there can be different message queues, each is identified by its own id.

There can be different types of messages in the message queue, and unlike pipe, the message queue does not strictly follow the FIFO mechanism.

Illustrative Diagram

The above image represents a message queue having two different types of messages 1 and 2.

Recommended Topic, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

Properties of message queues

  • Message queues are used for inter-process communication between related or unrelated processes.
  • Since there can be multiple message queues present, each message queue is identified by a message queue identifier.
  • There is no requirement that some processes should wait for a message to arrive in the queue before some processes can write a message to that queue.
  • Message queue does not necessarily implement FIFO every time.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

System call used for IPC using message queues

  1. msgget()

In Order to facilitate inter-process communication using message queues, the processes involved in the IPC should at least have access to a message queue. Through this system call, a new message queue is created, or in case if a message queue is already available, it is allocated.

Syntax:

int msgget(key_t key, int flag);

  • Key-value is associated with the message queue.
  • The flag represents various read-write values like IPC_CREAT or IPC_CREAT|IPC_EXCL

It returns an integer identifier that is used to refer to a newly created queue or an existing queue based on the specified key.

2. msgsnd()

Once the processes have a message queue, the messages have to be put onto the queue. The messages are put onto the queue using the msgsnd() function.

Syntax:

int msgsnd (int msgid, const void *ptr, size_t length, int flag)

  • msgid is an identifier returned by the msgget function.
  • ptr represents a pointer to a structure with the following template, which is defined in  <sys/msg.h>

struct msgbuf

{

long mtype;

char mtext[1];

};

  • The message type must be positive ( greater than zero).
  • length specifies the size of the message in bytes.
  • The flag argument can be 0 or IPC_NOWAIT.

3. msgrcv()

Once the message is sent to the message queue, there has to be some mechanism by which other processes can read the message from the queue. This mechanism is facilitated by the msgrcv() function. The msgrcv() function is used to read a message from the message queue.

Syntax:

size_t msgrcv ( int msgid, void *ptr, size_t length, long type, int flag)

  • msgid is an identifier returned by the msgget function.
  • The ptr argument specifies the address where the received message is to be stored.
  • The length specifies the size of the data of the buffer pointed to by ptr. This does not include a long integer type field.
  • Type specifies the message on the queue which is sought
  1. If the type is 0, the very first message on the queue is returned.
  2. If the type is greater than 0, the first message whose type equals 'type' is returned.
  3. If the type is less than 0, the first message with the lowest type that is less than or equal to the absolute value of the 'type' argument is returned.
  • The flag represents what has to be done in case the message requested is unavailable. 

4.  msgctl()

The msgctl() functions provide a variety of control operations on the message queue. However, it is mostly used to deallocate the message queue after its work is over.

Syntax:

int msgctl (int msgid, int cmd, struct msqid_ds *buff)

  • The third parameter is not mandatory.
  • In the cmd field, IPC_RMID is used to remove the message queue specified by msgid from the system.
  • Messages in the queue are discarded.

Code Implementation

  • Code for process writing onto the message queue.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct msg_buffer {
   long msg_type;
   char msg[100];
} message;
main() {
   key_t my_key;
   int msg_id;
   my_key = ftok("progfile", 65); //used to create a unique key
   msg_id = msgget(my_key, 0666 | IPC_CREAT); // used to create message queue and return its id
   message.msg_type = 1;
   printf("Write Message : ");
   fgets(message.msg, 100, stdin);
   msgsnd(msg_id, &message, sizeof(message), 0); // used to send the message onto the queue
   printf("Sent message is : %s \n", message.msg);
}

 

  • Code for the process reading from the message queue.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// Define message queue structure
struct msg_buffer {
   long msg_type;
   char msg[100];
} message;
main() {
   key_t my_key;
   int msg_id;
   my_key = ftok("progfile", 65); //used to create a unique key
   msg_id = msgget(my_key, 0666 | IPC_CREAT); //used to create a message queue and return id
   msgrcv(msg_id, &message, sizeof(message), 1, 0); //used to receive message
   // display the message
   printf("Received Message is : %s \n", message.msg);
   msgctl(msg_id, IPC_RMID, NULL); //destroy (deallocate) the message queue
   return 0;
}

 

Also see - Difference between argument and parameter

Frequently Asked Questions

What are the system calls used in the message queue?

msgget(), msgsnd(), msgrcv(), msgctl()

What is a message queue?

The message queues can be thought of as a linked list of messages that is used by the communicating processes.

Which function returns an identifier to the message queue?

msgget()

Which function is used to write the message onto the message queue?

msgsnd()

Which function is used to read the message from the message queue?

msgrcv()

Take this awesome course from coding ninjas.

Conclusion

  • The message queues can be thought of as a linked list of messages that is used by the communicating processes.
  • The message queues are stored within the kernel.
  • Each message queue is identified by a message queue identifier.
  • Message queue does not necessarily implement FIFO every time.
  • The system calls used in the message queue are: msgget(), msgsnd(), msgrcv(), msgctl().
  • msgget()  returns an integer identifier that is used to refer to a newly created queue or an existing queue, based on the specified key.
  • The messages are put onto the queue using the msgsnd() function.
  • The msgrcv() function is used to read a message from the message queue.
  • The msgctl() functions provide a variety of control operations on the message queue.


Recommended Readings: 


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Never stop learning. Explore more here.

Happy learning!

Previous article
IPC through Shared Memory
Next article
Communication between Two Processes using Signals
Live masterclass