本文介绍了消息队列(IPC)接收过程不在C中打印接收的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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?

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







now msgbuf.h

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

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







谢谢:)

推荐答案

sizeof(rbuf->m)


这篇关于消息队列(IPC)接收过程不在C中打印接收的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:42