/* NAME: shmreaderOffLine SYNOPSIS: shmreaderOffLine < inputfile of two lines DESCRIPTION: This program communicate with another program (shmwriter) using shared memory. This program reads two lines the stdin into memory buffers and at the same time or later, shmwriter writes the buffers into stdout. */ #include "def.h" BUFFER *buf[2]; int shmid[2]; main() { int i; /* creat the shared memory segments and attach them */ for (i = 0; i < 2; i++) { if ( (shmid[i] = shmget(SHMKEY + i, sizeof(BUFFER), PERMS | IPC_CREAT)) < 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); } /* read two lines from stdin and write to memory */ buf[0]->size = read(0, buf[0]->data, BSIZE); buf[1]->size = read(0, buf[1]->data, BSIZE); /* detach this shared memory and close the semaphores. the other program (shmwriter) is the last one to use the shared memory, so it'll remove it when it's done */ for (i = 0; i < 2; i++) { if (shmdt((void *) buf[i]) < 0) printf("server: can't detach shared memory "); } exit(0); }