/* * Write a string to a shared memory segment of 512 bytes * New File Mar 22,1989 * Author Jerry LeVan * */ #include #include #include #include #include #include #define SIZE 512 /* shared memory buffer size */ #define KEY 'JHL ' /* our key */ main() { key_t key; int memoryid; char * location; /* the shmget will fail if the segment has not been created */ memoryid = shmget(KEY,SIZE,0); printf("status of key retrival = %d\n",memoryid); if(memoryid == -1) {perror("Bad Key");exit();} /* we got the key now attach to the segment we will allow the system to chose the location (second parm) and we will want read/write access (third parm) */ location = (char *)shmat(memoryid,0,0); /* we want read write access */ printf("status of attach = %d\n",location); if (location == (char*)-1) {perror("Bad Attach");exit();} /* memory has been attached to,now copy string into the location */ printf("Enter string -> "); gets(location); printf("location contains: %s\n",location); }