Design and Develop a program to demonstrate pipe function using dup system call.

AIM:

Design and Develop a program to demonstrate pipe function using  dup system call.

Program:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
   int ptd[2];
   char buff[30];
   if(pie(ptd)==-1)
   {
      perror(“pipe failed”);
      exit(1);
   }
   if(!fork())
   {
      printf(“CHILD:writing to the pipe\n”);
      write(ptd[1],”test”,5);
      printf(“CHILD: exiting\n”);
      exit(0);
    }
    else
    {
      printf(“PARENT:reading from pipe\n”);
      read(ptd[0],buf,5);
      printf(“PARENT: read \”%S “\n”,buff);
      wait(NULL);
    }
}

Output:

Student@ubuntu:~$ gcc –o  dup.out dup.c
Student@ubuntu:~$./dup.out
PARENT:reading from pipe
CHILD:writing to the pipe
CHILD: exiting
PARENT: read testInitial Value.