#include <stdio.h>     // for printf, NULL
#include <sys/types.h> // for fork
#include <unistd.h>    // for pipe, fork, read, write

int main()
{
  int p[2];

  if(pipe(p) == -1) {
      printf("Pipe creation failed ...\n");
      exit(1);
  }

  /* Fork a child, set the pipe to flow from parent to child */
  if(fork() == 0) /* child */
  {
      close(p[1]);               /* close the write end of the pipe */ 
      dup2(p[0],0);              /* redirect the standard input to the pipe */
      execlp("bc", "bc", NULL);  /* invoke the arbitrary precision calculator bc */
                                 /* bc inherits the pipe and the File Descriptor Table from the child */
  }
  else /* parent */
  {
      close(p[0]);               /* close the read end of the pipe */
      write(p[1], "2*3*4\n", strlen("2*3*4\n")); /* pass an arithmetic expression to bc */ 
      write(p[1], "quit\n", strlen("quit\n"));   /* terminate the bc process */
      close(p[1]);               /* send EOF (not used by bc) */
      wait(NULL);                /* wait for child to finish executing */ 
  }
}