在派则mq_se​​nd结构 [英] send struct in mq_send

查看:117
本文介绍了在派则mq_se​​nd结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用POSIX IPC,并根据文件 - <一个href=\"http://man7.org/linux/man-pages/man3/mq_send.3.html\">http://man7.org/linux/man-pages/man3/mq_send.3.html

I am using POSIX IPC and according to the documentation - http://man7.org/linux/man-pages/man3/mq_send.3.html

则mq_se​​nd()方法只发送字符*数据和mq_recv()临危只有字符数据。
不过,我想自定义的结构发到我的味精队列,在接收端,我想获得的结构。

mq_send() method only sends char* data and mq_recv() recieves only character data. However, I want to send a custom struct to my msg queue and on the receiving end, I want to get the struct.

样的结构:

struc Req
{
  pid_t pid;
  char data[4096];
}

所以,没有人知道如何在C浪做到这一点?

So, does anyone know how to accomplish this in C lang?

推荐答案

您只需要通过结构的地址,并将它转换为相应的指针类型:为const char *为则mq_se​​nd 和char *为则mq_receive

You just need to pass the address of the struct and cast it to the appropriate pointer type: const char * for mq_send and char * for mq_receive.

typedef struct Req
{
  pid_t pid;
  char data[4096];
} Req;

Req buf;

n = mq_receive(mqdes0, (char *) &buf, sizeof(buf), NULL);

mq_send(mqdes1, (const char *) &buf, sizeof(buf), 0);

这篇关于在派则mq_se​​nd结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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