IPC消息队列如何发送对的向量 [英] IPC message queues how to send a vector of pairs

查看:244
本文介绍了IPC消息队列如何发送对的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在两个进程之间发送和接收消息
结构如下

I'm trying to send and receive a message between 2 processes the struct as follows

struct _st{
    long _var1;
    int _var2; 
    int _var3; 
    int _var4;
    int _var5;
    vector <pair<int,int> > _var6; 
};

,我的发送代码为

send_val = msgsnd(msgqid, &message, sizeof(message), !IPC_NOWAIT);

我这样收到

rec_val = msgrcv(msgqid, &message, sizeof(message), 0, !IPC_NOWAIT);

当我将我的_var6从接收到的消息分配给另一个变量并打印它的值
i get垃圾。

when i assign my _var6 from the received message to another variable and print it's values i get garbage.

如何正确地发送和接收此结构?

How can i send and receive this struct correctly?

推荐答案

_st 不是POD序列化/反序列化它。你实际上发送_var6内部指针超过IPC不是它的内容。

_st is not POD you cant' simply send over IPC without serialize/deserialize it. You are actually sending _var6 internal pointer over IPC not its content.

sizeof(message) code> _st struct size,它不包括 _var6 的内容大小。
您需要在msgsnd之前手动序列化 _st ,并在msgrcv之后对其进行反序列化。

sizeof(message) will only get static _st struct size, it doesn't include contents size of _var6. You need to serialize _st manually before msgsnd and deserialize it after msgrcv.

http://www.boost.org/doc/libs /1_52_0/libs/serialization/doc/index.html

这篇关于IPC消息队列如何发送对的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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