pipes/simple_pipe.c

This is pipes/simple_pipe.c (Listing 44-2, page 896), an example from the book, The Linux Programming Interface.

The source code file is copyright 2010, 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
+/* simple_pipe.c
+
+   Simple demonstration of the use of a pipe to communicate
+   between a parent and a child process.
+
+   Usage: simple_pipe "string"
+
+   The program creates a pipe, and then calls fork() to create a child process.
+   After the fork(), the parent writes the string given on the command line
+   to the pipe, and the child uses a loop to read data from the pipe and
+   print it on standard output.
+*/
 #include <sys/wait.h>
 #include "tlpi_hdr.h"
 
 #define BUF_SIZE 10
 
 int
 main(int argc, char *argv[])
 {
     int pfd[2];                             /* Pipe file descriptors */
     char buf[BUF_SIZE];
     ssize_t numRead;
 
     if (argc != 2 || strcmp(argv[1], "--help") == 0)
         usageErr("%s string\n", argv[0]);
 
     if (pipe(pfd) == -1)                    /* Create the pipe */
         errExit("pipe");
 
     switch (fork()) {
     case -1:
         errExit("fork");
 
     case 0:             /* Child  - reads from pipe */
         if (close(pfd[1]) == -1)            /* Write end is unused */
             errExit("close - child");
 
         for (;;) {              /* Read data from pipe, echo on stdout */
             numRead = read(pfd[0], buf, BUF_SIZE);
             if (numRead == -1)
                 errExit("read");
             if (numRead == 0)
                 break;                      /* End-of-file */
             if (write(STDOUT_FILENO, buf, numRead) != numRead)
                 fatal("child - partial/failed write");
         }
 
         write(STDOUT_FILENO, "\n", 1);
         if (close(pfd[0]) == -1)
             errExit("close");
         _exit(EXIT_SUCCESS);
 
     default:            /* Parent - writes to pipe */
         if (close(pfd[0]) == -1)            /* Read end is unused */
             errExit("close - parent");
 
         if (write(pfd[1], argv[1], strlen(argv[1])) != strlen(argv[1]))
             fatal("parent - partial/failed write");
 
         if (close(pfd[1]) == -1)            /* Child will see EOF */
             errExit("close");
         wait(NULL);                         /* Wait for child to finish */
         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.