c - mq_send 中的发送结构

标签 c linux posix ipc mqueue

我正在使用 POSIX IPC 并根据文档 - http://man7.org/linux/man-pages/man3/mq_send.3.html

mq_send() 方法仅发送 char* 数据,而 mq_recv() 仅接收字符数据。 但是,我想将自定义结构发送到我的消息队列,并且在接收端,我想获取该结构。

示例结构:

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

那么,有谁知道如何在 C 语言中实现这一点?

最佳答案

您只需传递结构的地址并将其转换为适当的指针类型:mq_send 为 const char *,mq_receive 为 char *。

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);

关于c - mq_send 中的发送结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23044963/

相关文章:

c - C堆栈变量是否反向存储?

c - 从文本文件的第一行读取整数数组,如果超过 10 则引发错误

linux - POSIX 将用户输入可接受的字符数限制为 4096,如何增加?

c - sigaction 和 signal 有什么区别?

python - 在 C 中,如何在没有嵌套函数的情况下为一个函数提供另一个函数的作用域?

c - 枚举和结构定义

php - 从命令行和 max_execution_time 运行 PHP 脚本

linux - 在 Linux 中将 QApplication 与 gTest 结合使用

linux - LD_PRELOAD 在动态库加载器中导致段错误

c - 为什么我会得到大的收获整数?