IPC using Message Queues (original) (raw)

Last Updated : 11 Aug, 2025

**Inter-Process Communication (IPC) is a method for communication within processes to exchange data, control information, or synchronizing tasks. There are various methods available for inter-process communication (IPC), like IPC using shared memory, message passing, semaphores, sockets, and message queues.

Message Queue is the most common way for IPC, which allow processes to send and receive messages in a queue-like manner.

Message Queues

A Message Queue is a linked list of messages stored within the kernel and identified by a message queue identifier and it is used to send and receive messages between processes in a **first-in-first-out (FIFO) manner.

The **msgget() function is used to create a new queue or open an existing queue and new messages are added to the end of a queue by using **msgsnd().

In a message queue every message has a positive long integer type field, a non-negative length, and the actual data bytes (corresponding to the length), all of which are specified to msgsnd() when the message is added to a queue. Messages are fetched from a queue by **msgrcv().

**Note: We don't have to fetch the messages in a first-in, first-out order. Instead, we can fetch messages based on their type field.

IPC Using Message Queues

The Inter-Process Communication (IPC) is essential for the enabling processes to the communicate and synchronize with the each other in an operating system. Among the various IPC mechanisms message queues provide the robust and flexible method for the processes to exchange data in a queued orderly fashion. It enables asynchronous communication, where messages are placed in a queue and retrieved by processes in a convenient manner.

**Message Queue for Writer/Sender Process

C `

#include <stdio.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAX 10

// structure for message queue struct mesg_buffer { long mesg_type; char mesg_text[100]; } message;

int main() { key_t key; int msgid;

// ftok to generate unique key
key = ftok("progfile", 65);

// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);

// msgsnd to send message
msgsnd(msgid, &message, sizeof(message), 0);

// display the message
printf("Data send is : %s \n", message.mesg_text);

return 0;

}

`

**Message Queue for Reader/Reciever Process

C `

#include <stdio.h> #include <sys/ipc.h> #include <sys/msg.h>

// structure for message queue struct mesg_buffer { long mesg_type; char mesg_text[100]; } message;

int main() { key_t key; int msgid;

// ftok to generate unique key
key = ftok("progfile", 65);

// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message
msgrcv(msgid, &message, sizeof(message), 1, 0);

// display the message
printf("Data Received is : %s \n", 
                message.mesg_text);

// to destroy the message queue
msgctl(msgid, IPC_RMID, NULL);

return 0;

}

`

**Output

Working of IPC using Message Queue

  1. **Creating/Accessing a Message Queue: The first step is to creatr or access a message queue using the **msgget() function. The msgget function creates a new meaage queue if the queue does not exists.
  2. **Sending the Message: After successfully creating/accessing the message queue a process can now send a message to the queue using **msgsnd() function which will be stored in the message queue in a format that contains message type and actual message content.
  3. **Receiving the Message: Once the message is added in the queue, it can be retrieved from the queue by another process using msgrcv(). This function allows the receiving process to specify the message type it wants to read. The message is then removed from the queue once it is received.
  4. **Delete the Message Queue: We can use the msgctl() function to delete the message queue if it is no longer needed.

Functions of Message Queue

The Message queues are powerful IPC tools that allow processes to send and receive messages in the FIFO (First In, First Out) order. Here are the primary functions of the message queues:

**System calls used for message queues: