ASYNCHRONOUS MESSAGE QUEUE
I AM A NEWBIE IN MESSAGE QUEUE. i have made a normal message queue of
sender and receiver. now i want to make asynchronous message queue but i
am not able to understand the logic My sender and reciever program of
normal message queue is
sender
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf
{
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msgid;
key_t key ;
char a[100];
int msgflg=IPC_CREAT |0666;
message_buf sbuf;
size_t buf_length;
key =11;
if((msgid=msgget(key,msgflg))<0)
{
perror("msgget");
exit(1);
}
sbuf.mtype=1;
printf("enter the message \n");
scanf("%s",a);
strcpy(sbuf.mtext,a);
buf_length=strlen(sbuf.mtext) + 1 ;
if(msgsnd(msgid,&sbuf,buf_length,IPC_NOWAIT)<0){
printf("msg send error");
exit(1);
}
else
{
printf("message is : %s",sbuf.mtext);
}
exit (0);
}
Receiver :
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
`enter code here`#include <stdio.h>
# define MSGSZ 100
typedef struct msgbuf
{
long mtype ;
char mtext[MSGSZ];
}message_buf ;
main()
{
int msgid ;
int msgflag;
key_t key ;
message_buf rbuf ;
key =11;
if ((msgid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}
if (msgrcv(msgid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
exit(1);
}
printf("at the reciever :%s\n", rbuf.mtext);
exit(0);
}
No comments:
Post a Comment