pipes/pipe_sync.c

This is pipes/pipe_sync.c (Listing 44-3, page 897), 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
+/* pipe_sync.c
+
+   Show how pipes can be used for synchronizing the actions of a parent and
+   multiple child processes.
+
+   Usage: pipe_sync sleep-time...
+
+   After creating a pipe, the program creates one child for each command-line
+   argument. Each child simulates doing some work by sleeping for the number of
+   seconds specified in the corresponding command-line argument. When it has
+   finished doing its "work", each child closes its file descriptor for the
+   write end of the pipe; the parent can see that all children have finished
+   their work when it sees end-of-file on the read end of the pipe.
+*/
 #include "curr_time.h"                      /* Declaration of currTime() */
 #include "tlpi_hdr.h"
 
 int
 main(int argc, char *argv[])
 {
     int pfd[2];                             /* Process synchronization pipe */
     int j, dummy;
 
     if (argc < 2 || strcmp(argv[1], "--help") == 0)
         usageErr("%s sleep-time...\n", argv[0]);
 
     setbuf(stdout, NULL);                   /* Make stdout unbuffered, since we
                                                terminate child with _exit() */
     printf("%s  Parent started\n", currTime("%T"));
 
     if (pipe(pfd) == -1)
         errExit("pipe");
 
     for (j = 1; j < argc; j++) {
         switch (fork()) {
         case -1:
             errExit("fork %d", j);
 
         case 0: /* Child */
             if (close(pfd[0]) == -1)        /* Read end is unused */
                 errExit("close");
 
             /* Child does some work, and lets parent know it's done */
 
             sleep(getInt(argv[j], GN_NONNEG, "sleep-time"));
                                             /* Simulate processing */
             printf("%s  Child %d (PID=%ld) closing pipe\n",
                     currTime("%T"), j, (long) getpid());
             if (close(pfd[1]) == -1)
                 errExit("close");
 
             /* Child now carries on to do other things... */
 
             _exit(EXIT_SUCCESS);
 
         default: /* Parent loops to create next child */
             break;
         }
     }
 
     /* Parent comes here; close write end of pipe so we can see EOF */
 
     if (close(pfd[1]) == -1)                /* Write end is unused */
         errExit("close");
 
     /* Parent may do other work, then synchronizes with children */
 
     if (read(pfd[0], &dummy, 1) != 0)
         fatal("parent didn't get EOF");
     printf("%s  Parent ready to go\n", currTime("%T"));
 
     /* Parent can now carry on to do other things... */
 
     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