Inter-Process Communication Using Message Queue
Inter-Process Communication Using Message Queue
1️⃣ What is a Message Queue?
A message queue is an IPC mechanism that allows processes to exchange messages in the form of structured data.
Unlike pipes:
-
Processes need not be related
-
Messages are stored in the kernel
-
Communication is asynchronous
2️⃣ Types of Message Queues in Linux
-
System V Message Queues (commonly taught in OS labs)
-
POSIX Message Queues (advanced)
👉 This example uses System V message queues.
3️⃣ System Calls Used
| System Call | Purpose |
|---|---|
msgget() | Create or access message queue |
msgsnd() | Send a message |
msgrcv() | Receive a message |
msgctl() | Control / delete queue |
4️⃣ Message Structure
Every message must begin with a long integer message type.
5️⃣ Communication Model (Example)
-
Sender process sends a message
-
Receiver process receives and prints it
6️⃣ Small Example: Message Queue IPC
🔹 Sender Program
🔹 Receiver Program
7️⃣ How to Run
8️⃣ Sample Output
Sender:
Receiver:
9️⃣ Important Points
-
Message queues allow structured communication
-
Messages have types
-
Kernel stores messages until received
-
Processes need same key
🟦 Explanation of Functions Used in Message Queue Program
We will explain the functions used in sender and receiver programs:
1️⃣ ftok() — Generate Unique Key
Prototype
Purpose
-
Generates a unique IPC key
-
Same key must be used by both sender and receiver
Arguments
| Argument | Meaning |
|---|---|
pathname | Existing file path |
proj_id | Project identifier (integer/character) |
Example
Important Notes
-
File must exist
-
Used to ensure both processes refer to the same message queue
📌 Exam Tip:
ftok()converts a filename and project ID into a unique key.
2️⃣ msgget() — Create or Access Message Queue
Prototype
Purpose
-
Creates a new message queue or
-
Accesses an existing message queue
Arguments
| Argument | Meaning |
|---|---|
key | Key generated by ftok() |
msgflg | Permission + creation flags |
Example
Flags Explained
-
0666→ Read/write permission -
IPC_CREAT→ Create queue if it does not exist
📌 Return Value
-
Returns message queue ID
-
-1on failure
3️⃣ msgsnd() — Send Message to Queue
Prototype
Purpose
-
Sends a message to the queue
Arguments
| Argument | Meaning |
|---|---|
msqid | Message queue ID |
msgp | Pointer to message structure |
msgsz | Size of message text (excluding long) |
msgflg | Control flags |
Example
Important Notes
-
msg_typemust be first field -
msgszexcludeslong msg_type
📌 Common Mistake
4️⃣ msgrcv() — Receive Message from Queue
Prototype
Purpose
-
Receives a message from the queue
Arguments
| Argument | Meaning |
|---|---|
msqid | Message queue ID |
msgp | Pointer to buffer |
msgsz | Size of message text |
msgtyp | Type of message to receive |
msgflg | Control flags |
Example
Message Type Rules
msgtyp | Meaning |
|---|---|
>0 | Receive specific type |
0 | Receive first message |
<0 | Receive lowest type ≤ abs |
📌 Blocking Behavior
-
Blocks until message arrives (unless
IPC_NOWAITused)
5️⃣ msgctl() — Control Message Queue
Prototype
Purpose
-
Control or delete the message queue
Arguments
| Argument | Meaning |
|---|---|
msqid | Message queue ID |
cmd | Control command |
buf | Optional structure |
Example
Common Commands
| Command | Description |
|---|---|
IPC_RMID | Remove message queue |
IPC_STAT | Get status |
IPC_SET | Set permissions |
📌 Exam Tip
Always remove the message queue to avoid resource leakage.
6️⃣ struct msg_buffer — Message Structure
Definition
Rules
-
msg_typemust be long -
First field is mandatory
-
Used to identify message category
7️⃣ Supporting Library Headers
| Header | Purpose |
|---|---|
<sys/ipc.h> | IPC key definitions |
<sys/msg.h> | Message queue functions |
<string.h> | String operations |
<stdio.h> | Input/output |
Comments
Post a Comment