Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am implementing message queue mechanism for IPC in C linux. Below is my receiving process. It's not printing the message received. It's generating a valid msqid, and other parameters of msgrcv funtion are also correct, I think. Why so?

C#
//header files
#include"msgbuf.h"
int main()
{
   int msqid;
   key_t key;
   int msgflg = 0666;
   message_buf  *rbuf;
   rbuf=malloc(sizeof(*rbuf));
   rbuf->m=malloc(sizeof(M1));
   key = ftok("/home/user",12);
   if ((msqid = msgget(key, msgflg)) ==-1)
   {
        perror("msgget");
        exit(1);
   }
   printf("\n\n%d\n",msqid);  //working fine till here.
   /* Receive an answer of message type 1.   */
   if (msgrcv(msqid, &rbuf, sizeof(rbuf->m), 1, 0) < 0)
   {
        perror("msgrcv");
        exit(1);
   }
   /* Print the answer.  */
   printf("Received message text= %s\n", rbuf->m->cp);
   return 0;
}




now msgbuf.h

C#
typedef struct msgclient
{
  int msglen;
  int msgtype;
  char *cp;
}M1;

typedef struct msgbuf1
{
   long    mtype;
   M1      *m;
} message_buf;



thanks :)
Posted

1 solution

You should know this is probably 4 (or 8 on 64 bit) and not the same as sizeof (struct M1).
C++
sizeof(rbuf->m)
 
Share this answer
 
Comments
Babita22 31-Mar-14 1:10am    
Thanks @bling That's one of the reasons. but the main problem with my prog was one can not use *m even, to send data stored in m to the receiving process because of separate address spaces of differenct processes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900