Palindrome Checking Using Message Queue

The first process sends a string to the second process. The second process reverses the received string and sends it back to the first process. The first process compares the original string and the reversed string received from the second one and then prints whether the string is a palindrome or not.

MESSAGE QUEUE – STRING REVERSAL & PALINDROME CHECK



1. AIM

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

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

  2. Process 2 reverses the string and sends it back.

  3. 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:

FunctionPurpose
msgget()            Create or access a message queue
msgsnd()            Send a message
msgrcv()            Receive a message
msgctl()            Control operations (delete queue)

Communication steps:

  1. Process 1 → sends string (msgsnd)

  2. Process 2 → receives string, reverses it, sends back

  3. Process 1 → receives reversed string and checks palindrome


3. ALGORITHM

Process 1 (Sender)

  1. Create message queue using msgget().

  2. Read a string from the user.

  3. Send the string to Process 2 (msgsnd).

  4. Wait and receive the reversed string (msgrcv).

  5. Compare original and reversed string.

  6. Print whether the string is a palindrome.

  7. Remove the message queue (msgctl).


Process 2 (Receiver)

  1. Access the same message queue using msgget().

  2. Receive string sent by Process 1.

  3. Reverse the string.

  4. 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.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command