Suppose I have a father process p1
and its child process p2
. p1
creates p2
and lets p
2 do something then stores the result into a std::map
. Now p1
wants to access the map.
This is inter-process communication, I want to use shared memory to do that. I'm thinking of two solutions.
p2 dumps the map to a char array and writes it to the shared memory and then p1 reads the shared memory to reconstruct the map. The type of the map is
<std::string, double>
, but I am not sure how to dump it to a char array.In p2, suppose the pointer to the shared memory is
void *shm
, can I allocate the memory of the map using this waystd::map<std::string, double>* result = (std::map<std::string, double>*)shm
and then insert elements toresult
? In p1, I can do the same thingstd::map<std::string, double>* result = (std::map<std::string, double>*)shm
and then iterate the map. But I am not sure whether it is correct.