Blame manual/examples/pipe.c

Packit 6c4009
/* Creating a Pipe
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
/* Read characters from the pipe and echo them to @code{stdout}.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
read_from_pipe (int file)
Packit 6c4009
{
Packit 6c4009
  FILE *stream;
Packit 6c4009
  int c;
Packit 6c4009
  stream = fdopen (file, "r");
Packit 6c4009
  while ((c = fgetc (stream)) != EOF)
Packit 6c4009
    putchar (c);
Packit 6c4009
  fclose (stream);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Write some random text to the pipe. */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
write_to_pipe (int file)
Packit 6c4009
{
Packit 6c4009
  FILE *stream;
Packit 6c4009
  stream = fdopen (file, "w");
Packit 6c4009
  fprintf (stream, "hello, world!\n");
Packit 6c4009
  fprintf (stream, "goodbye, world!\n");
Packit 6c4009
  fclose (stream);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  pid_t pid;
Packit 6c4009
  int mypipe[2];
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
  /* Create the pipe.  */
Packit 6c4009
  if (pipe (mypipe))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Pipe failed.\n");
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
  /* Create the child process.  */
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == (pid_t) 0)
Packit 6c4009
    {
Packit 6c4009
      /* This is the child process.
Packit 6c4009
	 Close other end first.  */
Packit 6c4009
      close (mypipe[1]);
Packit 6c4009
      read_from_pipe (mypipe[0]);
Packit 6c4009
      return EXIT_SUCCESS;
Packit 6c4009
    }
Packit 6c4009
  else if (pid < (pid_t) 0)
Packit 6c4009
    {
Packit 6c4009
      /* The fork failed.  */
Packit 6c4009
      fprintf (stderr, "Fork failed.\n");
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This is the parent process.
Packit 6c4009
	 Close other end first.  */
Packit 6c4009
      close (mypipe[0]);
Packit 6c4009
      write_to_pipe (mypipe[1]);
Packit 6c4009
      return EXIT_SUCCESS;
Packit 6c4009
    }
Packit 6c4009
}