#include #include #include #include #include #include #include #include #include using namespace std; const int SIZE{4096}; const string NAME{"/SharedMem"}; int main( int , char ** ) { int openFlags { O_RDONLY}; 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; } int protect{ PROT_READ}; char* basePtr = reinterpret_cast ( mmap(0, SIZE, protect, MAP_SHARED, fd, 0)); close(fd); // note to everyone, always read the man page for return type. if(basePtr == MAP_FAILED) { perror("\tError mmap: "); } else { printf("%s\n", (char * ) basePtr); int i = 0; while (basePtr[i] != 0) { cout << char (basePtr[i]) << endl; ++i; } cout << endl; } if ( -1 == shm_unlink(NAME.c_str())) { perror("\tError shm_unlink: "); return 1; } return 0; }