/* Creating a shared memory segment and attaching it to the logical address space. sharedMem1.c++ $ g++ -Wall sharedMem1.c++ $ ./a.out w $ ./a.out r */ #include using namespace std; #include #include #include #include #define SIZE 4 int main(int count, char *vect[]) { int shmID, *p ; if(count < 2) { cerr << "No 2nd argument\n"; exit(1) ; } shmID = shmget(ftok("/home/goutam", 1234), SIZE, IPC_CREAT | 0777); if(shmID == -1) { cerr << "Error in shmget" ; exit(1) ; } p = (int *) shmat(shmID, 0, 0777) ; cout << "Attached at VA: " << p << endl; if(vect[1][0] == 'w') { cout << "Enter an integer: "; cin >> *p ; // Write data shmdt(p) ; } else if(vect[1][0] == 'r'){ cout << "The data is:" << *p << "\n"; shmdt(p) ; } // The shared memory segment remains in the system // $ ipcs // $ ipcrm -m return 0 ; }