/* shmWriter can be used with shmReader or with CopyFile. With CopyFile, it is the child process that writes the buffers into stdout. With shmReader, this program writes the buffers into stdout. */ #include "def.h" BUFFER *buf[2]; int shmid[2]; main() { int i; /* get the shared memory segments and attach it the parent must have already created it */ for (i = 0; i < 2; i++) { if ( (shmid[i] = shmget(SHMKEY + i, sizeof(BUFFER), 0 )) < 0) printf("server: can't get shared memory %d", i); if ( (buf[i] = (BUFFER *) shmat(shmid[i], (char *) 0, 0)) == (BUFFER *) -1) printf("server: can't attach shared memory %d", i); } write(1, buf[0]->data, buf[0]->size); write(1, buf[1]->data, buf[1]->size); /* * Detach and remove the shared memory segments and * close the semaphores. */ for (i = 0; i < 2; i++) { if (shmdt((void *) buf[i]) < 0) printf("client: can't detach shared memory "); if (shmctl(shmid[i], IPC_RMID, (struct shmid_ds *) 0) < 0) printf("client: can't remove shared memory "); } exit(0); }