#include <sys/wait.h> 
#include <stdio.h>
#include <stdlib.h>   
#include <unistd.h>   
#include <string.h>
#include <errno.h>

int main() {
  int fd[2], errno; 
  char buf;       
  const char* msg = "Hello world .. Have a nice day!!\n"; 
  if (pipe(fd) < 0) {
     printf("Error Number : %d\n", errno); 
     perror("Error Description"); 
     return (-1); } 
  pid_t cpid = fork();                                
  if (cpid < 0) { 
     printf("Error Number : %d\n", errno); 
     perror("Error Description"); 
     return (-2); } 
  // child process
  if (cpid==0) {                    
    // child only reads, so close write end
    close(fd[1]);                        
    while (read(fd[0], &buf, 1) > 0)       
           write(STDOUT_FILENO, &buf, sizeof(buf));        
    close(fd[0]);                          
    _exit(0); }                                       
  // parent process
  else {              
    // parent only writes, so close read end
    close(fd[0]);                          
    write(fd[1], msg, strlen(msg));       
    close(fd[1]);                         
    wait(NULL);                                       
    exit(0); }
  return 0; }
  
  
  

