Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  ex1.c
Language: C/C++
Code:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
 int main()
{
     int p1[2],p2[2];
     int temp,d;
     //char a[10];
     pid_t t1,t2,pid1,pid2;
     if((pipe(p1)==-1)&&(pipe(p2)==-1))
     printf("not created..");
     t1=fork();
     t2=fork();
     if(t1>0&&t2>0)
     {
     d=getpid();temp=3;
     printf("Node 1:%dn",getpid());
     printf("child 1:%dn",t1);
     pid1=t1;
     printf("child 2:%dn",t2);
     pid2=t2;
     printf("enter child value:");
     scanf("%d",&temp);
     if(temp==1)
     {write(p1[1],&temp,4);
     t1=0;t2=2;}
     else
        {t2=0;t1=2;
     write(p1[1],&temp,4);}
     
     }

      if(temp==pid1)

     {
          
      printf("1");
       
             {printf("2");
          printf("child 1:n");
          printf("c1 parent id:%dn",getppid());
          printf("c1 node 2:%dn",getpid());
          read(p1[0],&temp,4);
          printf("c1 output : %dn",temp);
          
     }}
     
     if(temp==pid2)
     {     
          printf("3");
          if((t2==0)||(t1!=0))
          {printf("4");

     printf("child 2:n");
     printf("c2 parent id:%dn",getppid());
     printf("c2 node 2:%dn",getpid());
     read(p2[0],&temp,4);
     printf("c2 output : %dn",temp);
          }
     }
     /* if(temp==3)
     printf("child 3:%d",getpid());
     else
     printf("nil");*/
return 0;

          
Comments: