System call used for IPC using message queues
- 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
- If the type is 0, the very first message on the queue is returned.
- If the type is greater than 0, the first message whose type equals 'type' is returned.
- 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);
}

You can also try this code with Online C Compiler
Run Code
- 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;
}

You can also try this code with Online C Compiler
Run Code
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!