pointers - shared C++ object through memory -
i have quick question familiar inter-process communication.
situation
- i have program (program a) can add code to, limited. main program generate lot of data.
- the way data formulated limited, create second program (program b) , hence need data b. , cause run functions no return value.
- i aware of named pipes, feel might bulky? - not sure though - have instance following concerns (may unfounded):
- data flow => convert binary -> place data in memory -> server read -> convert string -> through switch statement determine requested -> requested -> convert binary -> place in memory -> read client , convert string / acceptable format.
- it has use switch statements on both sides , if want different format of info other string, need take consideration
- one message might have wait complete, might slower during lot of calls @ same time? - not sure though
- other inter process communication methods has same problem.
better solution think create "object" - class. , share object memory address between programs, thereby theoretically "merging" , b then:
- there no problem encode , decode issues etc
- data requested / invoked through calling function.
- the function return proper type , no need establish correct type (i.e. bool / int / string / double etc)
i understand has several problems, i.e. if object gets removed memory location main / program accessing it.
question
- what best way solve problems:
- is there invoke option in c++ allow me write , read memory address? @ moment:
- i can access same object between , b, can't write / read throw exception. can through simple invoke perhaps read / write object?
- i aware of writeprocessmemory function - not want - i.e. don't want change memory values, merely access data / invoke actions b perform.
- is there simple , easy way of doing this? aware of called boost, don't know - best option? -> i.e. should investigate best solution?
thank in advance advise on issue.
boost.interprocess had many ways share data between processes, 1 of shared_memory
example taken boost, program acts server or client same memory object (depending on whether argument given or not)
include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <cstring> #include <cstdlib> #include <string> int main(int argc, char *argv[]) { using namespace boost::interprocess; if(argc == 1){ //parent process //remove shared memory on construction , destruction struct shm_remove { shm_remove() { shared_memory_object::remove("mysharedmemory"); } ~shm_remove(){ shared_memory_object::remove("mysharedmemory"); } } remover; //create shared memory object. shared_memory_object shm (create_only, "mysharedmemory", read_write); //set size shm.truncate(1000); //map whole shared memory in process mapped_region region(shm, read_write); //write memory 1 std::memset(region.get_address(), 1, region.get_size()); //launch child process std::string s(argv[0]); s += " child "; if(0 != std::system(s.c_str())) return 1; } else{ //open created shared memory object. shared_memory_object shm (open_only, "mysharedmemory", read_only); //map whole shared memory in process mapped_region region(shm, read_only); //check memory initialized 1 char *mem = static_cast<char*>(region.get_address()); for(std::size_t = 0; < region.get_size(); ++i) if(*mem++ != 1) return 1; //error checking memory } return 0; }
Comments
Post a Comment