This is svsem/svsem_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 47.

The source code file is copyright 2022, 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.


Download svsem/svsem_create.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* svsem_create.c

   Experiment with the use of semget() to create a System V semaphore set.

   Usage as shown in usageError().
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.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} "
                            "num-sems [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");
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;

    while ((opt = getopt(argc, argv, "cf:k:px")) != -1) {
        switch (opt) {
        case 'c':
            flags |= IPC_CREAT;

        case 'f':               /* -f pathname */
            key = ftok(optarg, 1);
            if (key == -1)

        case 'k':               /* -k key (octal, decimal or hexadecimal) */
            if (sscanf(optarg, "%li", &lkey) != 1)
                cmdLineErr("-k option requires a numeric argument\n");
            key = lkey;

        case 'p':
            key = IPC_PRIVATE;

        case 'x':
            flags |= IPC_EXCL;

            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], "Must specify number of semaphores\n");

    int numSems = getInt(argv[optind], 0, "num-sems");

    unsigned int perms = (argc <= optind + 1) ? (S_IRUSR | S_IWUSR) :
                         getInt(argv[optind + 1], GN_BASE_8, "octal-perms");

    int semid = semget(key, numSems, flags | perms);
    if (semid == -1)

    printf("%d\n", semid);      /* On success, display semaphore set ID */


Download svsem/svsem_create.c

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.

Valid XHTML 1.1