Beginner-friendly guide to Message Queue in Linux. Learn working, applications, advantages, disadvantages, and real-world use cases with examples.
Introduction
When multiple processes in Linux need to talk to each other, they use a mechanism called Inter-Process Communication (IPC). One of the most commonly used IPC methods is the Message Queue in Linux.
A message queue allows processes to send and receive messages in a structured way. Think of it like a post office: one process writes a message and drops it into the queue, and another process picks it up when needed. This helps in asynchronous communication, where processes don’t have to wait for each other to run at the same time.
What is a Message Queue in Linux?
A message queue in Linux is a kernel-managed data structure that stores messages. Each message is identified by a type and can carry data in the form of text or binary.
- Asynchronous Communication → The sender can post a message without waiting for the receiver.
- Orderly Messaging → Messages are delivered in the order they are placed, unless prioritized by message type.
- Safe and Structured → Unlike shared memory, message queues reduce the risk of data corruption because the kernel manages the queue.
How Message Queue Works in Linux
- Create a Message Queue – A queue is created with a unique key.
- Send a Message – A process writes a message into the queue.
- Receive a Message – Another process reads the message when it’s ready.
- Remove the Queue – Once communication is done, the queue is deleted.
This whole process is handled using system calls like msgget, msgsnd, msgrcv, and msgctl.
Advantages of Message Queue in Linux
✅ Simple way for processes to exchange data.
✅ Works asynchronously (no need for sender and receiver to be active together).
✅ Provides message prioritization.
✅ Reduces data corruption risks compared to shared memory.
Disadvantages of Message Queue in Linux
❌ Limited message size and queue length defined by the system.
❌ Kernel overhead may affect performance in heavy-load applications.
❌ Not suitable for real-time communication where speed is critical.
❌ Messages may be lost if queues are not managed properly.
Real-Time Applications of Message Queue in Linux
- Operating Systems → IPC between different services.
- Embedded Systems → Communication between processes in automotive or IoT devices.
- Client-Server Models → Passing requests and responses asynchronously.
- Telecommunication → Handling multiple messages in switching systems.
Example (Conceptual)
Imagine you have two processes:
- Process A (sender) → puts a message “Hello” in the queue.
- Process B (receiver) → later reads the “Hello” message when it’s free.
This way, Process A doesn’t wait for B, and B can pick the message anytime.
Example: Message Queue in Linux using C
Linux supports System V message queues. Below is an example showing how one process can send a message and another can receive it.
We’ll write two programs:
1. Sender Program (msg_sender.c)
#include
#include
#include
#include
#include
// Structure for message
struct msg_buffer {
long msg_type;
char msg_text[100];
};
int main() {
struct msg_buffer message;
key_t key;
int msgid;
// Generate a unique key
key = ftok("progfile", 65);
// Create a message queue and return identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.msg_type = 1; // Message type must be > 0
printf("Enter a message to send: ");
fgets(message.msg_text, sizeof(message.msg_text), stdin);
// Send message to queue
msgsnd(msgid, &message, sizeof(message.msg_text), 0);
printf("Message sent: %s\n", message.msg_text);
return 0;
}
2. Receiver Program (msg_receiver.c)
#include
#include
#include
#include
// Structure for message
struct msg_buffer {
long msg_type;
char msg_text[100];
};
int main() {
struct msg_buffer message;
key_t key;
int msgid;
// Generate the same key as sender
key = ftok("progfile", 65);
// Access the message queue
msgid = msgget(key, 0666 | IPC_CREAT);
// Receive message
msgrcv(msgid, &message, sizeof(message.msg_text), 1, 0);
// Display message
printf("Message received: %s\n", message.msg_text);
// Destroy the message queue after reading
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
Steps to Run the Code
- Save the files as
msg_sender.candmsg_receiver.c. - Create a dummy file
progfile(used byftokfor generating the key):touch progfile - Compile both programs:
gcc msg_sender.c -o sender gcc msg_receiver.c -o receiver - Run the sender in one terminal:
./sender(Type a message and press Enter.) - Run the receiver in another terminal:
./receiver
You’ll see the message displayed by the receiver.
This demonstrates asynchronous IPC using Message Queue in Linux.
- The sender writes a message into the queue.
- The receiver fetches it from the queue.
- After receiving, the queue is destroyed to clean up resources.
Applications of Message Queue in Linux
Message Queues are widely used in real-world systems where multiple processes need to exchange information efficiently. Some common applications are:
- Operating Systems → Internal services like logging, scheduling, and event handling often use message queues.
- Embedded Systems → Automotive ECUs, IoT devices, and robotics use message queues to manage tasks and share sensor data.
- Telecommunication Systems → Handling multiple simultaneous calls or messages.
- Client-Server Applications → Sending requests from clients and responses from servers asynchronously.
- Banking and Financial Applications → Queues handle millions of transaction messages securely and in order.
- Distributed Systems → Used to pass tasks and results between processes running on different nodes.
Advanced Advantages of Message Queue in Linux
Apart from the basic benefits (like asynchronous communication and structured data), here are some advanced advantages:
Prioritization Support → Messages can be categorized by type, allowing important messages to be handled first.
Kernel-Level Security → The Linux kernel controls access, so unauthorized processes cannot tamper with queues.
Multiple Consumers → A single queue can serve multiple receiving processes, improving scalability.
Synchronization-Free → Unlike shared memory, you don’t need semaphores/mutexes for synchronization.
Reliability → Messages are safely stored in the queue until a receiver picks them up.
Advanced Disadvantages of Message Queue in Linux
While useful, message queues also have limitations, especially in high-performance systems:
Limited Capacity → The maximum size of a message and total number of messages in a queue are restricted by system limits.
Performance Overhead → Since the kernel manages queues, context switching adds overhead compared to faster methods like shared memory.
Complex Error Handling → If a receiver crashes before reading a message, handling recovery can be tricky.
Not Real-Time Friendly → Due to kernel overhead, message queues are not suitable for hard real-time applications.
Resource Leaks → If a queue is not removed properly (msgctl), it may consume system resources unnecessarily.
When to Use Message Queue in Linux
- Use when processes don’t need to run at the same time (asynchronous).
- Use when structured communication with prioritization is required.
- Use in multi-client or multi-server environments where multiple processes communicate through one channel.
Avoid message queues when:
- Very large data needs to be exchanged → use Shared Memory instead.
- Real-time performance is critical → use Signals or RTOS queues.
- System needs low latency communication → use Pipes or Sockets.
FAQs on Message Queue in Linux
Q1: Is Message Queue in Linux same as FIFO or Pipe?
No. FIFO/Pipe is a one-way communication channel, while message queues allow structured two-way communication with prioritization.
Q2: What system calls are used for message queues in Linux?
Common system calls: msgget, msgsnd, msgrcv, msgctl.
Q3: Can message queues handle large data?
Message queues are better for small structured messages. For large data, shared memory is preferred.
Q4: Are message queues available in all Linux distributions?
Yes, System V and POSIX message queues are supported across major Linux systems.
Conclusion
The Message Queue in Linux is a reliable and beginner-friendly way to implement inter-process communication. It provides asynchronous, ordered, and structured messaging between processes. While it has some limitations like size and performance overhead, it is widely used in operating systems, embedded systems, and client-server models.
If you are new to Linux IPC, message queues are an excellent starting point to understand how processes talk to each other
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.









