F. Message Queue in Unix Using C Programming

MESSAGE QUEUE

CLIENT:


#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);
}
}

SERVER:


#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);
}

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");
}