pmsg/pmsg_create.c

This is pmsg/pmsg_create.c (Listing 52-2, page 1069), an example from the book, The Linux Programming Interface.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU 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.

  Cover of The Linux Programming Interface
+/* pmsg_create.c
+
+   Create a POSIX message queue.
+
+   Usage as shown in usageError().
+
+   Linux supports POSIX message queues since kernel 2.6.6.
+*/
 #include <mqueue.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "tlpi_hdr.h"
 
 static void
 usageError(const char *progName)
 {
     fprintf(stderr, "Usage: %s [-cx] [-m maxmsg] [-s msgsize] mq-name "
             "[octal-perms]\n", progName);
     fprintf(stderr, "    -c          Create queue (O_CREAT)\n");
     fprintf(stderr, "    -m maxmsg   Set maximum # of messages\n");
     fprintf(stderr, "    -s msgsize  Set maximum message size\n");
     fprintf(stderr, "    -x          Create exclusively (O_EXCL)\n");
     exit(EXIT_FAILURE);
 }
 
 int
 main(int argc, char *argv[])
 {
     int flags, opt;
     mode_t perms;
     mqd_t mqd;
     struct mq_attr attr, *attrp;
 
+    /* If 'attrp' is NULL, mq_open() uses default attributes. If an
+       option specifying a message queue attribute is supplied on the
+       command line, we save the attribute in 'attr' and set 'attrp'
+       pointing to 'attr'. We assign some (arbitrary) default values
+       to the fields of 'attr' in case the user specifies the value
+       for one of the queue attributes, but not the other. */
+
     attrp = NULL;
     attr.mq_maxmsg = 10;
     attr.mq_msgsize = 2048;
     flags = O_RDWR;
 
     /* Parse command-line options */
 
     while ((opt = getopt(argc, argv, "cm:s:x")) != -1) {
         switch (opt) {
         case 'c':
             flags |= O_CREAT;
             break;
 
         case 'm':
             attr.mq_maxmsg = atoi(optarg);
             attrp = &attr;
             break;
 
         case 's':
             attr.mq_msgsize = atoi(optarg);
             attrp = &attr;
             break;
 
         case 'x':
             flags |= O_EXCL;
             break;
 
         default:
             usageError(argv[0]);
         }
     }
 
     if (optind >= argc)
         usageError(argv[0]);
 
     perms = (argc <= optind + 1) ? (S_IRUSR | S_IWUSR) :
                 getInt(argv[optind + 1], GN_BASE_8, "octal-perms");
 
     mqd = mq_open(argv[optind], flags, perms, attrp);
     if (mqd == (mqd_t) -1)
         errExit("mq_open");
 
     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.

Valid XHTML 1.1