我正在C linux中实现IPC的消息队列机制。以下是我的接收流程。它不打印收到的消息。我认为它正在生成有效的msqid,而其他msgrcv函数参数也是正确的。为什么这样?

//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;
}


现在msgbuf.h

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


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

最佳答案

if (msgrcv(msqid, &rbuf, sizeof(rbuf->m), 1, 0) < 0)


应该

if (msgrcv(msqid, &rbuf, sizeof(struct message_buf), 1, 0) < 0)

关于c - 消息队列(IPC)接收过程未在C中打印接收到的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22631997/

10-10 12:42