C Program to show an example of Indirect Communication


 

C Program to show an example of Indirect Communication
recieve:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/msg.h>
struct my_msgbuf
{
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int i=ftok("snd.c",'B');
int msqid=msgget(i,0644);
for(;;)
{
msgrcv(msqid,&buf,sizeof(buf.mtext),0,0);
printf("Recieving from message queue:%s\n",buf.mtext);
}
return(0);
}

send:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
struct my_msgbuf
{
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int i=ftok("snd.c",'B');
int msqid=msgget(i,0644|IPC_CREAT);
printf("Enter the message to put into message queue,ctrl+c to quit:\n");
buf.mtype=1;
while(fgets(buf.mtext,sizeof(buf.mtext),stdin)!=NULL)
{
int len=strlen(buf.mtext);
if(buf.mtext[len-1]=='\n')
buf.mtext[len-1]='\0';
msgsnd(msqid,&buf,len+1,0);
}
return 0;
}

Post a Comment

Previous Post Next Post