消息队列(IPC)接收过程不在C中打印接收的数据 [英] message queue(IPC) receiving process not printing the received data in C

查看:180
本文介绍了消息队列(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;







谢谢:)

推荐答案

你应该知道这可能是4(或64位上的8)并且与sizeof(struct M1)不一样。

You should know this is probably 4 (or 8 on 64 bit) and not the same as sizeof (struct M1).
sizeof(rbuf->m)


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆