Message Queue in Unix Using C Programming
🎯 Introduction
Message queues are a crucial inter-process communication mechanism used in Unix-based systems for sending and receiving messages between processes. They provide a reliable and efficient way to transfer data and synchronize activities between different programs running concurrently. In this blog post, we will explore the concept of Message Queues in Unix and demonstrate how to implement them using the C programming language. We'll cover the fundamental concepts, the server-client architecture, a detailed explanation of the code, and the output generated by the provided C program.
🎯 Concept of Message Queue
A message queue is a communication method in which processes exchange data through messages placed in a common queue. The sender process sends a message to the queue, and the receiver process reads the message from the queue. This decouples the communication between processes, allowing them to interact without needing to know each other's details.
Message queues are useful for scenarios where different processes need to exchange data asynchronously and where it is essential to ensure data integrity and proper synchronization. They are commonly used in multi-process and multi-threaded environments to facilitate communication between various components of a system.
🎯 Detailed Explanation Client
👉 Client
The client code connects to the existing message queue using the msgget system call. It then enters an infinite loop where it continuously receives messages from the queue using msgrcv. The message received contains the name of the file to be read. The client opens the specified file using open and reads its content using read. The message's type is updated to the client's process ID (pid), and the message is sent back to the server using msgsnd.
 👉 Client CodeÂ
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int type;
char a[1024];
int pid;
}p;
int main()
{
int m,n,fd,m1;
m=msgget(KEY,0666|IPC_CREAT);
while(1)
{
msgrcv(m,&p,sizeof(p),1,0);
printf("Filename from client %s\n",p.a);
fd=open(p.a,O_RDONLY);
n=read(fd,p.a,1024);
p.type=p.pid;
p.pid=getpid();
msgsnd(m,&p,sizeof(p),0);
}
}
🎯 Detailed Explanation Server
👉 Server
The server code also connects to the existing message queue using the msgget system call. It then sends a message to the queue, requesting the client to provide the filename to be processed. The server prompts the user to enter a message and stores it in the message buffer. The server's process ID (pid) is also stored in the message. This message is then sent to the client using msgsnd. The server then waits to receive the processed message back from the client using msgrcv. Finally, it prints the content of the received message, which is the output of the processed file.
 👉 Source CodeÂ
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int type;
char a[1024];
int pid;
}p,p1;
int main()
{
int m;
m=msgget(KEY,0);
p.type=1;
printf("\nEnter the msg");
scanf("%s",&p.a);
pid_t pid;
p.pid=getpid();
msgsnd(m,&p,sizeof(p),0);
msgrcv(m,&p1,sizeof(p),p.pid,0);
printf("%s",p1.a);
}
🎯 Sample Output
Enter the msg strcmp.c
Filename from client strcmp.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char *argv[])
{
        if(strcmp(argv[1],argv[2])==0)
                printf("The given strings are equal");
        else
                printf("The strings are not equal");
}
🎯 Key Points
The client-server model enables asynchronous communication between processes using a message queue.
The msgget function is used to obtain the message queue identifier. The same identifier is used by both the client and the server to connect to the same queue.
The msgrcv function is used to receive messages from the queue, while msgsnd is used to send messages to the queue.
The message structure includes a type field that is used to identify the type of the message. In this code, p.type is set to 1 in the server to request the client to provide the filename.
The message's payload contains a character array (a) to store the filename and an integer (pid) to store the process ID.
🎯 Summary
Message queues in Unix provide an efficient and reliable way to facilitate communication between processes in a decoupled manner. The client-server architecture demonstrated in the provided C code showcases a basic implementation of message queues. The client reads a file, sends the content back to the server for processing, and the server displays the result. This example illustrates the core concepts of message queues in Unix.
Overall, message queues are a powerful tool for inter-process communication, and they play a significant role in various concurrent and distributed systems. Understanding and effectively using message queues can lead to robust and scalable applications that can handle complex communication scenarios in Unix environments.