#include "myhdr.h"

#define MSGSIZE 16
char *msg1 = "Hello, World! ";
char *msg2 = "How do you do?";
char *msg3 = "I am a  stream";

main()
{
  char inbuf[MSGSIZE];
  int p[2];  // pipe descriptor
  int j, retVal;

  // make sure syscall pipe () was successful  
  if (pipe(p) < 0)
    {
      perror("Pipe call");
      return(1);
    }

  /* Assert: p[1] to write to, p[0] is to read from */
  // make sure fork sys call went thru'

  sleep(3);

  if ((retVal = fork()) < 0) {
    perror("fork call");
    return(2);
  }

  printf("*************Return value after fork is %d\n", retVal);
  if (retVal > 0) 
    {
  /**************************this is Parent code ********************/
      printf(" Parent: My Pid is %d \n", getpid());
      printf ("**************PARENT: going to sleep\n");
      sleep(2);
      write(p[1],msg1,MSGSIZE);
      printf ("**************PARENT: going to sleep\n");
      sleep(5);
      write(p[1],msg2,MSGSIZE);
      printf ("**************PARENT: going to sleep\n");
      sleep(10);
      write(p[1],msg3,MSGSIZE);
     wait((int *)0);
  /******************parent code ends here ***********************/

  }

  if (retVal == 0)
    /*****************child code starts here********************/
    {
      printf("Child:  My Pid is %d \n", getpid());

      for (j = 0; j < 3; j++)
	{ printf("CHILD: waiting to read\n");
          read(p[0],inbuf, MSGSIZE);
	  printf("CHILD: %s\n", inbuf);
	}
      /***********child code ends here ************************/
    }
  return 0;
}
