This is svshm/svshm_xfr_reader.c (Listing 48-3, page 1005), an example program file from the book The Linux Programming Interface.
The source code file is copyright 2010, Michael Kerrisk, and is licensed under the GNU Affero General Public License, version 3.
This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below.
In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.
+/* svshm_xfr_reader.c + + Read data from a System V shared memory using a binary semaphore lock-step + protocol; see svshm_xfr_writer.c +*/ #include "svshm_xfr.h" int main(int argc, char *argv[]) { int semid, shmid, xfrs, bytes; struct shmseg *shmp; /* Get IDs for semaphore set and shared memory created by writer */ semid = semget(SEM_KEY, 0, 0); if (semid == -1) errExit("semget"); shmid = shmget(SHM_KEY, 0, 0); if (shmid == -1) errExit("shmget"); + /* Attach shared memory read-only, as we will only read */ + shmp = shmat(shmid, NULL, SHM_RDONLY); if (shmp == (void *) -1) errExit("shmat"); /* Transfer blocks of data from shared memory to stdout */ for (xfrs = 0, bytes = 0; ; xfrs++) { if (reserveSem(semid, READ_SEM) == -1) /* Wait for our turn */ errExit("reserveSem"); if (shmp->cnt == 0) /* Writer encountered EOF */ break; bytes += shmp->cnt; if (write(STDOUT_FILENO, shmp->buf, shmp->cnt) != shmp->cnt) fatal("partial/failed write"); if (releaseSem(semid, WRITE_SEM) == -1) /* Give writer a turn */ errExit("releaseSem"); } if (shmdt(shmp) == -1) errExit("shmdt"); /* Give writer one more turn, so it can clean up */ if (releaseSem(semid, WRITE_SEM) == -1) errExit("releaseSem"); fprintf(stderr, "Received %d bytes (%d xfrs)\n", bytes, xfrs); exit(EXIT_SUCCESS); }
This page copyright (C) 2013, Michael Kerrisk, mtk AT man7.org