svmsg/svmsg_receive.cThis is svmsg/svmsg_receive.c (Listing 46-3, page 945), 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.
|
/* svmsg_receive.c Usage: svmsg_receive [-nex] [-t msg-type] msqid [max-bytes] Experiment with the msgrcv() system call to receive messages from a System V message queue. See also svmsg_send.c. */ #define _GNU_SOURCE /* Get definition of MSG_EXCEPT */ #include <sys/types.h> #include <sys/msg.h> #include "tlpi_hdr.h" #define MAX_MTEXT 1024 struct mbuf { long mtype; /* Message type */ char mtext[MAX_MTEXT]; /* Message body */ };
static void usageError(const char *progName, const char *msg) { if (msg != NULL) fprintf(stderr, "%s", msg); fprintf(stderr, "Usage: %s [options] msqid [max-bytes]\n", progName); fprintf(stderr, "Permitted options are:\n"); fprintf(stderr, " -e Use MSG_NOERROR flag\n"); fprintf(stderr, " -t type Select message of given type\n"); fprintf(stderr, " -n Use IPC_NOWAIT flag\n"); #ifdef MSG_EXCEPT fprintf(stderr, " -x Use MSG_EXCEPT flag\n"); #endif exit(EXIT_FAILURE); }
int main(int argc, char *argv[]) { int msqid, flags, type; ssize_t msgLen; size_t maxBytes; struct mbuf msg; /* Message buffer for msgrcv() */ int opt; /* Option character from getopt() */ /* Parse command-line options and arguments */ flags = 0; type = 0; while ((opt = getopt(argc, argv, "ent:x")) != -1) { switch (opt) { case 'e': flags |= MSG_NOERROR; break; case 'n': flags |= IPC_NOWAIT; break; case 't': type = atoi(optarg); break; #ifdef MSG_EXCEPT case 'x': flags |= MSG_EXCEPT; break; #endif default: usageError(argv[0], NULL); } } if (argc < optind + 1 || argc > optind + 2) usageError(argv[0], "Wrong number of arguments\n"); msqid = getInt(argv[optind], 0, "msqid"); maxBytes = (argc > optind + 1) ? getInt(argv[optind + 1], 0, "max-bytes") : MAX_MTEXT; /* Get message and display on stdout */ msgLen = msgrcv(msqid, &msg, maxBytes, type, flags); if (msgLen == -1) errExit("msgrcv"); printf("Received: type=%ld; length=%ld", msg.mtype, (long) msgLen); if (msgLen > 0) printf("; body=%s", msg.mtext); printf("\n"); 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.