MESSAGE QUEUE – STRING REVERSAL & PALINDROME CHECK

1. AIM
To implement interprocess communication using System V Message Queues, where:

Process 1 (Sender) sends a string to Process 2 (Receiver).

Process 2 reverses the string and sends it back.

Process 1 compares the original and reversed strings to determine whether the string is a palindrome.

2. THEORY
Message queues allow processes to exchange structured messages. They support:

Bidirectional communication

Asynchronous message transfer

Typed messages (each message has a type)

System V message queue APIs used:

Function	Purpose
msgget()	            Create or access a message queue
msgsnd()	            Send a message
msgrcv()	            Receive a message
msgctl()	            Control operations (delete queue)
Communication steps:

Process 1 → sends string (msgsnd)

Process 2 → receives string, reverses it, sends back

Process 1 → receives reversed string and checks palindrome

3. ALGORITHM
Process 1 (Sender)
Create message queue using msgget().

Read a string from the user.

Send the string to Process 2 (msgsnd).

Wait and receive the reversed string (msgrcv).

Compare original and reversed string.

Print whether the string is a palindrome.

Remove the message queue (msgctl).

Process 2 (Receiver)
Access the same message queue using msgget().

Receive string sent by Process 1.

Reverse the string.

Send reversed string back.

4. PROGRAM
File 1: sender.c (Process 1)
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSGKEY 1234

struct msg {
    long type;
    char text[100];
};

int main() {
    struct msg m1, m2;
    int qid;

    qid = msgget(MSGKEY, IPC_CREAT | 0666);

    printf("Enter a string: ");
    scanf("%s", m1.text);

    m1.type = 1;
    msgsnd(qid, &m1, sizeof(m1.text), 0);

    msgrcv(qid, &m2, sizeof(m2.text), 2, 0);

    printf("Reversed string received: %s\n", m2.text);

    if (strcmp(m1.text, m2.text) == 0)
        printf("Palindrome\n");
    else
        printf("Not a palindrome\n");

    msgctl(qid, IPC_RMID, NULL);
    return 0;
}
File 2: receiver.c (Process 2)
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSGKEY 1234

struct msg {
    long type;
    char text[100];
};

void reverse(char *s) {
    int l = 0, r = strlen(s) - 1;
    while (l < r) {
        char temp = s[l];
        s[l] = s[r];
        s[r] = temp;
        l++; r--;
    }
}

int main() {
    struct msg m1, m2;
    int qid;

    qid = msgget(MSGKEY, IPC_CREAT | 0666);

    msgrcv(qid, &m1, sizeof(m1.text), 1, 0);

    reverse(m1.text);

    m2.type = 2;
    strcpy(m2.text, m1.text);
    msgsnd(qid, &m2, sizeof(m2.text), 0);

    return 0;
}
5. COMPILATION & EXECUTION
Open two terminals:

Terminal 1 – Run Receiver
gcc receiver.c -o receiver
./receiver
Terminal 2 – Run Sender
gcc sender.c -o sender
./sender
6. SAMPLE OUTPUT
Sender Terminal
Enter a string: madam
Reversed string received: madam
Palindrome
Receiver Terminal
(Shows no direct output; works in background)
7. RESULT
The program successfully demonstrates interprocess communication using System V Message Queues, where:

One process sends a string,

Another process reverses it and sends it back,

The first process checks if it is a palindrome.
