#include <stdio.
h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_SIZE 1024
// Define a structure for the message
struct message {
long mtype; // Message type
char mtext[MAX_SIZE]; // Message data
};
int main() {
int msqid;
key_t key;
struct message msg;
// Generate a key for the message queue
key = ftok(".", 'b');
if (key == -1) {
perror("ftok");
exit(1);
}
// Create a message queue
msqid = msgget(key, IPC_CREAT | 0666);
if (msqid == -1) {
perror("msgget");
exit(1);
}
// Write data to the message queue
printf("Enter a message to send: ");
fgets([Link], MAX_SIZE, stdin);
[Link] = 1; // Message type can be any non-zero positive integer
if (msgsnd(msqid, &msg, strlen([Link]) + 1, 0) == -1) {
perror("msgsnd");
exit(1);
}
printf("Message sent.\n");
// Read data from the message queue
if (msgrcv(msqid, &msg, MAX_SIZE, 1, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("Message received: %s", [Link]);
// Remove the message queue
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
return 0;
}