问题
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