#include #include #include #include #include #include #include #include #include #include "Matrix.hpp" using namespace boost::interprocess; typedef allocator ShmAllocator_float; typedef Matrix ShmMatrix; int main(int argc, char *argv[]) { if(argc == 1) { // parent process std::cout << "parent process" << std::endl << std::endl; //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMatrix"); } ~shm_remove(){ shared_memory_object::remove("MySharedMatrix"); } } remover; // create shared memory managed_shared_memory segment(create_only, "MySharedMatrix", 100000); // Matrix const ShmAllocator_float floatAllocator(segment.get_segment_manager()); ShmMatrix* pmat = segment.construct("matrix")(2, 2, floatAllocator); pmat->operator()(0, 0) = 0; pmat->operator()(0, 1) = 1; pmat->operator()(1, 0) = 2; pmat->operator()(1, 1) = 3; std::cout << "Matrix size = " << pmat->rows() << " x " << pmat->columns() << std::endl; std::cout << pmat->operator()(0, 0) << "\t" << pmat->operator()(0, 1) << std::endl; std::cout << pmat->operator()(1, 0) << "\t" << pmat->operator()(1, 1) << std::endl; std::cout << std::endl; //Launch child process std::string s(argv[0]); s += " child "; if(0 != std::system(s.c_str())) return 1; std::cout << "parent returning" << std::endl; } else { // child process std::cout << "child process" << std::endl; try { managed_shared_memory segment(open_only, "MySharedMatrix"); std::pair mat_info = segment.find("matrix"); ShmMatrix* pmat = mat_info.first; std::cout << "Matrix size = " << pmat->rows() << " x " << pmat->columns() << std::endl; std::cout << pmat->operator()(0, 0) << "\t" << pmat->operator()(0, 1) << std::endl; std::cout << pmat->operator()(1, 0) << "\t" << pmat->operator()(1, 1) << std::endl; std::cout << std::endl; } catch(...) { std::cout << "error opening shared memory" << std::endl; } std::cout << "child returning" << std::endl; } return 0; }