pipes/fifo_seqnum_client.cThis is pipes/fifo_seqnum_client.c (Listing 44-8, page 914), 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. |
+/* fifo_seqnum_client.c + + A simple client that uses a well-known FIFO to request a (trivial) + "sequence number service". This client creates its own FIFO (using a + convention agreed upon by client and server) which is used to receive a reply + from the server. The client then sends a request to the server consisting of + its PID and the length of the sequence it wishes to be allocated. The client + then reads the server's response and displays it on stdout. + + See fifo_seqnum.h for the format of request and response messages. + + The server is in fifo_seqnum_server.c. +*/ #include "fifo_seqnum.h" static char clientFifo[CLIENT_FIFO_NAME_LEN]; static void /* Invoked on exit to delete client FIFO */ removeFifo(void) { unlink(clientFifo); } int main(int argc, char *argv[]) { int serverFd, clientFd; struct request req; struct response resp; if (argc > 1 && strcmp(argv[1], "--help") == 0) usageErr("%s [seq-len]\n", argv[0]); /* Create our FIFO (before sending request, to avoid a race) */ umask(0); /* So we get the permissions we want */ snprintf(clientFifo, CLIENT_FIFO_NAME_LEN, CLIENT_FIFO_TEMPLATE, (long) getpid()); if (mkfifo(clientFifo, S_IRUSR | S_IWUSR | S_IWGRP) == -1 && errno != EEXIST) errExit("mkfifo %s", clientFifo); if (atexit(removeFifo) != 0) errExit("atexit"); /* Construct request message, open server FIFO, and send message */ req.pid = getpid(); req.seqLen = (argc > 1) ? getInt(argv[1], GN_GT_0, "seq-len") : 1; serverFd = open(SERVER_FIFO, O_WRONLY); if (serverFd == -1) errExit("open %s", SERVER_FIFO); if (write(serverFd, &req, sizeof(struct request)) != sizeof(struct request)) fatal("Can't write to server"); /* Open our FIFO, read and display response */ clientFd = open(clientFifo, O_RDONLY); if (clientFd == -1) errExit("open %s", clientFifo); if (read(clientFd, &resp, sizeof(struct response)) != sizeof(struct response)) fatal("Can't read response from server"); printf("%d\n", resp.seqNum); 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.