Write a C program
a. To create a message queue and
b. To send message to a message queue by mentioning message id,
message and message number
a. Receive message from message queue by mentioning message id and
message number.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
// Define the message structure
struct msg_buffer {
long msg_type; // Message type (must be > 0)
int msg_number; // Message number
char msg_text[100]; // Message content
};
int main() {
key_t key; // Message queue key
int msgid; // Message queue ID
struct msg_buffer message;
// Create a unique key for the message queue
key = ftok("msgqueue.c", 65); // Use the file "msgqueue.c" and project ID 65
if (key == -1) {
perror("ftok failed");
exit(1);
}
// Create the message queue
msgid = msgget(key, 0666 | IPC_CREAT); // 0666 gives read/write permission
if (msgid == -1) {
perror("msgget failed");
exit(1);
}
// Part (b): Send message to the message queue
printf("Enter message number: ");
scanf("%d", &message.msg_number);
getchar(); // To consume the newline character left by scanf
printf("Enter the message: ");
fgets(message.msg_text, sizeof(message.msg_text), stdin);
// Remove the newline character at the end of the input string
message.msg_text[strcspn(message.msg_text, "\n")] = '\0';
message.msg_type = 1; // Msg type is always > 0 (1 is a common choice)
// Send the message to the message queue
if (msgsnd(msgid, &message, sizeof(message) - sizeof(long), 0) == -1)
{
perror("msgsnd failed");
exit(1);
}
printf("Message sent: Type = %ld, Number = %d, Text = %s\n",
message.msg_type, message.msg_number, message.msg_text);
// Part (c): Receive message from the message queue
printf("Receiving message...\n");
// To receive the message from the queue
if (msgrcv(msgid, &message, sizeof(message) - sizeof(long), 1, 0) == -1) {
perror("msgrcv failed");
exit(1);
}
printf("Message received: Type = %ld, Number = %d, Text = %s\n",
message.msg_type, message.msg_number, message.msg_text);
// Remove the message queue
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl failed");
exit(1);
}
return 0;
}
Explanation:
1. Message Queue Creation:
o ftok() generates a unique key for the message queue using a file
(msgqueue.c in this case) and a project ID (65). This key is used by
msgget() to create a message queue.
o msgget() creates a message queue if it doesn't already exist or
retrieves the ID of an existing queue.
o 0666 | IPC_CREAT gives read and write permissions to the
message queue for all users.
2. Sending Message:
o The msg_buffer struct is used to store the message, which consists
of a message type (msg_type), a message number (msg_number),
and the message text (msg_text).
o msgsnd() sends the message to the queue. The sizeof(message) -
sizeof(long) is used to avoid counting the size of the long field
msg_type.
3. Receiving Message:
o msgrcv() is used to receive a message from the message queue.
The message type specified is 1 (this matches the type of message
sent earlier). The received message is then displayed.
4. Cleaning Up:
o The message queue is deleted using msgctl() with the IPC_RMID
option after the message is received.
Compilation and Execution:
1. Save the code in a file (e.g., msgqueue.c).
2. Compile the program using gcc:
gcc msgqueue.c –
./[Link]