#include #include #include #include #include #include #include #include #include using namespace std; const int SIZE{4096}; // look at /dev/shm const string NAME{"/SharedMem"}; int main( int argc, char * argv[]) { if (argc < 2) { cerr << "Usage " << argv[0] << " arg1 arg2 ..." << endl; return 0; } int openFlags {O_CREAT | O_RDWR | O_TRUNC}; mode_t mode{ S_IRUSR | S_IWUSR}; int fd{shm_open(NAME.c_str(), openFlags, mode)}; if (fd == -1) { perror("\tError shm_open: "); return 1; } if( -1 == ftruncate(fd, SIZE)) { perror("\tError ftruncate: "); shm_unlink(NAME.c_str()); return 1; } int protect{ PROT_READ| PROT_WRITE}; char* basePtr = reinterpret_cast ( mmap(0, SIZE, protect, MAP_SHARED, fd, 0)); close(fd); if(basePtr == MAP_FAILED) { perror("\tError mmap: "); } else { for(int i = 1; i < argc; ++i) { sprintf(basePtr, "%s ", argv[i]); basePtr += strlen(argv[i]) + 1; } } return 0; }