IPC message queues how to send a vector of pairs

对着背影说爱祢 提交于 2019-12-24 11:21:13

问题


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

and my sending code is

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

and i receive it this way

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

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?


回答1:


_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) 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.

have a look at http://www.boost.org/doc/libs/1_52_0/libs/serialization/doc/index.html



来源:https://stackoverflow.com/questions/13963653/ipc-message-queues-how-to-send-a-vector-of-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!