Saturday, 20 October 2012

Communication between two unrelated process using messege queue

MSGQ1.c 

#include<stdio.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{
int i=0;
int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Msg Q created sucessfully\n");
else
printf("Not created\n");
printf("Enter the msg type:\n");
scanf("%ld",&(ptr.mtype));

printf("Enter the msg\n");
scanf("%s",ptr.mtext);
int p=msgsnd(id,&ptr,(sizeof(ptr.mtext)),0);
if(p>=0)
printf("Msg send sucessfully\n");
else
printf("Msg not send\n");
}




MSGQ2.C



#include<stdio.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{

int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Success\n");
printf("Enter the msgtype\n");
scanf("%ld",&ptr.mtype);

int p=msgrcv(id,&ptr,30,(ptr.mtype),0);
if(p>=0)
{
printf("Msg read sucessfully\n");
printf("Msg is:%s\n",ptr.mtext);
}
else
printf("Error\n");
}


Sample output


msgq1.c

Enter the msg type:
2
Enter the msg
kkk
Msg send sucessfully


msgq2.c

Success
Enter the msgtype
2
Msg read sucessfully
Msg is:kkk





0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More