0
int main()
{
int x=10,y=20,z=0, p1[2], p2[2]; pipe(p1); pipe(p2);

if(!fork())
{
    close(p1[1]); close(p2[0]);
    while(read(p1[0],&z,sizeof(int)))
    {
        z=z*3;write(p2[1],&z,sizeof(int));
    }
    close(p1[0]); close(p2[1]); 
    if(!fork())
    {
        close(p2[1]); close(p1[0]); close(p1[1]);
        while(read(p2[0],&z,sizeof(int)))
        {
            z=z/2;
            printf("%d \n",z);
        }
        close(p2[0]); exit(0);
    }
    wait(0);
    exit(0);
}

close(p2[1]); close(p2[0]);
close(p1[0]);
write(p1[1],&x,sizeof(int)); write(p1[1],&y,sizeof(int));
close(p1[1]);
wait(0);
}

Where am I closing the file descriptors wrong? and my is my output a bunch of 0s instead of my intended value( first the value*3 in the child and then divide the value by 2 in the grandchild)? I first want to write the value of x and y to the first child through a pipe (p1) and then send the modified value to the grandchild through another pipe (p2).

LarmadVara
  • 47
  • 8

0 Answers0