svshm/svshm_create.cThis is svshm/svshm_create.c, an example to accompany the book, The Linux Programming Interface. This file is not printed in the book; it is a supplementary file for Chapter 48. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. 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_create.c Experiment with the use of shmget() to create a System V shared memory segment. Usage as shown in usageError(). */ #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/stat.h> #include "tlpi_hdr.h"
static void usageError(const char *progName, const char *msg) { if (msg != NULL) fprintf(stderr, "%s", msg); fprintf(stderr, "Usage: %s [-cx] {-f pathname | -k key | -p} " "seg-size [octal-perms]\n", progName); fprintf(stderr, " -c Use IPC_CREAT flag\n"); fprintf(stderr, " -x Use IPC_EXCL flag\n"); fprintf(stderr, " -f pathname Generate key using ftok()\n"); fprintf(stderr, " -k key Use 'key' as key\n"); fprintf(stderr, " -p Use IPC_PRIVATE key\n"); exit(EXIT_FAILURE); }
int main(int argc, char *argv[]) { /* Parse command-line options and arguments */ int numKeyFlags = 0; /* Counts -f, -k, and -p options */ int flags = 0; long lkey; key_t key; int opt; /* Option character from getopt() */ while ((opt = getopt(argc, argv, "cf:k:px")) != -1) { switch (opt) { case 'c': flags |= IPC_CREAT; break; case 'f': /* -f pathname */ key = ftok(optarg, 1); if (key == -1) errExit("ftok"); numKeyFlags++; break; case 'k': /* -k key (octal, decimal or hexadecimal) */ if (sscanf(optarg, "%li", &lkey) != 1) cmdLineErr("-k option requires a numeric argument\n"); key = lkey; numKeyFlags++; break; case 'p': key = IPC_PRIVATE; numKeyFlags++; break; case 'x': flags |= IPC_EXCL; break; default: usageError(argv[0], NULL); } } if (numKeyFlags != 1) usageError(argv[0], "Exactly one of the options -f, -k, " "or -p must be supplied\n"); if (optind >= argc) usageError(argv[0], "Size of segment must be specified\n"); int segSize = getLong(argv[optind], GN_ANY_BASE, "seg-size"); unsigned int perms = (argc <= optind + 1) ? (S_IRUSR | S_IWUSR) : getInt(argv[optind + 1], GN_BASE_8, "octal-perms"); int shmid = shmget(key, segSize, flags | perms); if (shmid == -1) errExit("shmget"); printf("%d\n", shmid); /* On success, display shared memory ID */ exit(EXIT_SUCCESS); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.