altio/poll_pipes.c

This is altio/poll_pipes.c (Listing 63-2, page 1340), 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
+/* poll_pipes.c
+
+   Example of the use of poll() to monitor multiple file descriptors.
+
+   Usage: poll_pipes num-pipes [num-writes]
+                                  def = 1
+
+   Create 'num-pipes' pipes, and perform 'num-writes' writes to
+   randomly selected pipes. Then use poll() to inspect the read ends
+   of the pipes to see which pipes are readable.
+*/
 #include <time.h>
 #include <poll.h>
 #include "tlpi_hdr.h"
 
 int
 main(int argc, char *argv[])
 {
     int numPipes, ready, randPipe, numWrites, j;
     struct pollfd *pollFd;
     int (*pfds)[2];                     /* File descriptors for all pipes */
 
     if (argc < 2 || strcmp(argv[1], "--help") == 0)
         usageErr("%s num-pipes [num-writes]\n", argv[0]);
 
     /* Allocate the arrays that we use. The arrays are sized according
        to the number of pipes specified on command line */
 
     numPipes = getInt(argv[1], GN_GT_0, "num-pipes");
     numWrites = (argc > 2) ? getInt(argv[2], GN_GT_0, "num-writes") : 1;
 
     pfds = calloc(numPipes, sizeof(int [2]));
     if (pfds == NULL)
         errExit("calloc");
     pollFd = calloc(numPipes, sizeof(struct pollfd));
     if (pollFd == NULL)
         errExit("calloc");
 
     /* Create the number of pipes specified on command line */
 
     for (j = 0; j < numPipes; j++)
         if (pipe(pfds[j]) == -1)
             errExit("pipe %d", j);
 
     /* Perform specified number of writes to random pipes */
 
     srandom((int) time(NULL));
     for (j = 0; j < numWrites; j++) {
         randPipe = random() % numPipes;
         printf("Writing to fd: %3d (read fd: %3d)\n",
                 pfds[randPipe][1], pfds[randPipe][0]);
         if (write(pfds[randPipe][1], "a", 1) == -1)
             errExit("write %d", pfds[randPipe][1]);
     }
 
     /* Build the file descriptor list to be supplied to poll(). This list
        is set to contain the file descriptors for the read ends of all of
        the pipes. */
 
     for (j = 0; j < numPipes; j++) {
         pollFd[j].fd = pfds[j][0];
         pollFd[j].events = POLLIN;
     }
 
     ready = poll(pollFd, numPipes, 0);
     if (ready == -1)
         errExit("poll");
 
     printf("poll() returned: %d\n", ready);
 
     /* Check which pipes have data available for reading */
 
     for (j = 0; j < numPipes; j++)
         if (pollFd[j].revents & POLLIN)
             printf("Readable: %3d\n", pollFd[j].fd);
 
     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