user1673892
user1673892

Reputation: 409

IPC message queues how to send a vector of pairs

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?

Upvotes: 1

Views: 1161

Answers (1)

billz
billz

Reputation: 45410

_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

Upvotes: 3

Related Questions