C PROGRAM THAT ILLUSTRATES THE FOLLOWING:
a. Creating a message queue.
b. Writing to a message queue.
c. Reading from a message queue.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include
<fcntl.h>
#define MAX 255
#define MAX 255
struct mesg
{
long type;
char mtext[MAX];
}*mesg;
char buff[MAX];
main()
{
int mid,fd,n,count=0;
if((mid=msgget(1006,IPC_CREAT | 0666))<0)
if((mid=msgget(1006,IPC_CREAT | 0666))<0)
{
printf("\n Can’t create Message Q");
printf("\n Can’t create Message Q");
exit(1);
}
printf("\n Queue id:%d", mid);
printf("\n Queue id:%d", mid);
mesg=(struct mesg
*)malloc(sizeof(struct mesg));
mesg->type=6;
fd=open("fact",O_RDONLY);
fd=open("fact",O_RDONLY);
while(read(fd,buff,25)>0)
{
strcpy(mesg->mtext,buff);
strcpy(mesg->mtext,buff);
if(msgsnd(mid,mesg,strlen(mesg->mtext),0)== -1)
printf("\n Message
Write Error");
}
if((mid=msgget(1006,0))<0)
{
printf("\n Can’t create
Message Q");
exit(1);
exit(1);
}
while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>0)
while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>0)
write(1,mesg->mtext,n);
count++;
count++;
if((n==-1)&(count==0))
printf("\n No Message Queue on Queue:%d",mid);
printf("\n No Message Queue on Queue:%d",mid);
}
0 comments:
Post a Comment