Message queue

Any one help me as i am new to c and message q programming.
problem is not able to print the char pointer data in receive program of message queue. here are my programs
/****send.c****/

#include
#include
#include
#include
#include
#include

struct mebufmsg
{
long msgtype ;
char *msgtext;
int data ;
char ch ;
};

int main(void)
{
struct mebufmsg *tbuff;

tbuff = (struct mebufmsg*)malloc(sizeof(struct mebufmsg)) ;

tbuff->msgtext = (char *)malloc(12*sizeof(char));

tbuff->msgtype = 1 ;
tbuff->msgtext = "sainath" ;
tbuff->data = 10 ;
tbuff->ch = 'a';

int msgqid;
key_t kiy=123;

size_t size = (sizeof(struct mebufmsg) - sizeof(tbuff->msgtype)) ;

printf("%d
", size ) ;


if ((msgqid = msgget(kiy, 0644 | IPC_CREAT)) == -1)
{
perror("Call to msgget() is failed!");
exit(1);
}

if (msgsnd(msgqid,tbuff, size, 0) == -1)
{
perror("msgsnd");
}

return 0;
}

/****receive.c *****/

#include
#include
#include
#include
#include
#include

struct mebufmsg
{
long msgtype ;
char *msgtext ;
int data ;
char ch ;
};
int main(void)
{
struct mebufmsg buffr;
int msgqid;
key_t kiy=123;

size_t size = (sizeof(buffr) - sizeof(buffr.msgtype));

printf("%d
", size ) ;


if ((msgqid = msgget(kiy, 0644)) == -1)
{ /* Connecting to the queue */
perror("msgget() system functionfailed");
exit(1);
}


if (msgrcv(msgqid, &buffr, size , 0, 0) == -1)
{
perror("msgrcv");
exit(1);
}
printf("%ld
", buffr.msgtype);
printf("%s
", buffr.msgtext);
printf("%d
", buffr.data);
printf("%c
", buffr.ch);

return 0;
}

printing some junk value from buffr.msgtext but able all other values.

so give some sugessions to fetch the buffr.msgtext as sainath.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories