Parent-child communication via pipes in C
<p>I am trying to get a parent process to accept user input for 4 integer variables (a, b, c, and x) and pipe them to the child process. The child process evaluates ax^2 + bx + c and pipes the result r to the parent, which displays it.</p>

<p>With the code I have now, any input results in a result of 1. The behavior of the print statements make me think only x is getting read, but even when I try to but x*x into r I don't get the right solution. What could I be doing wrong?</p>

<pre><code>#include &lt;stdio.h&gt; 
#include &lt;stdlib.h&gt; 
#include &lt;unistd.h&gt; 
#include &lt;string.h&gt; 
#include &lt;sys/wait.h&gt; 
#define PIPE_INPUT 0 
#define PIPE_OUTPUT 1 
#define INDENT 20 

void testError(int err, const char * str){ 
if ( err &lt; 0 ){ 
    perror(str); 
exit( EXIT_FAILURE ); 
}
} 

int main(){ 

int fd_toChild[2]; 
int fd_toParent[2]; 

int buff[20];

pid_t pid; 

testError( pipe(fd_toChild), "pipe" ); 
testError( pipe(fd_toParent), "pipe" ); 
testError( pid = fork(), "fork" ); 

int a; 
int b; 
int c; 
int x; 
int r; 

if (pid &gt; 0){     
    // this is the parent 


    int status;

    while(1){ 

        close( fd_toChild[PIPE_INPUT] ); 
        close( fd_toParent[PIPE_OUTPUT] ); 


        printf("This program will evaluate a polynomial a(x^2) + bx + c. \n"); 
        printf("Please input an integer value for a: "); 
        scanf("%d", &amp;a); 
        write(fd_toChild[PIPE_OUTPUT], &amp;a, sizeof(a)); 

        printf("Parent writes value %d to a \n", a); 
        printf("Please input an integer value for b: "); 
        scanf("%d", &amp;b); 
        write(fd_toChild[PIPE_OUTPUT], &amp;b, sizeof(b)); 
        printf("Parent writes value %d to b \n", b); 

        printf("Please input an integer value for c: "); 
        scanf("%d", &amp;c); 
        write(fd_toChild[PIPE_OUTPUT], &amp;c, sizeof(c)); 
        printf("Parent writes value %d to c \n", c); 

        printf("Please input an integer value for x: "); 
        scanf("%d", &amp;x); 
        write(fd_toChild[PIPE_OUTPUT], &amp;x, sizeof(x)); 
        printf("Parent writes value %d to x \n", x); 

        close( fd_toChild[PIPE_OUTPUT] ); 
        close( fd_toParent[PIPE_INPUT] ); 

        read(fd_toParent[PIPE_INPUT], buff, sizeof(r)); 

        pid = wait(&amp;status); 

        fprintf(stdout, "Parent received back the result %d.\n", r); 
        printf("The polynomial evaluates to %d \n", r); 

        close( fd_toParent[PIPE_OUTPUT] ); 

        break;
    }
} 

else{ 
    // this is child 

    close( fd_toChild[PIPE_OUTPUT] ); 
    close( fd_toParent[PIPE_INPUT] ); 

    while(1){ 
        close( fd_toChild[PIPE_OUTPUT] ); 
        close( fd_toParent[PIPE_INPUT] ); 

        read(fd_toChild[PIPE_INPUT], buff, sizeof(a)); 
        printf("Child reads value %d from a \n", a); 

        read(fd_toChild[PIPE_INPUT], buff, sizeof(b)); 
        printf("Child reads value %d from b \n", b); 

        read(fd_toChild[PIPE_INPUT], buff, sizeof(c)); 
        printf("Child reads value %d from c \n", c); 

        read(fd_toChild[PIPE_INPUT], buff, sizeof(x)); 
        printf("Child reads value %d from x \n", x); r = a * x * x + b * x + c; 
        printf("x is %d", x); printf("r is %d", r); 

        write(fd_toParent[PIPE_OUTPUT], buff, sizeof(r)); 
        fprintf(stdout, "%*sChild is all done\n", INDENT, ""); 
        close( fd_toChild[PIPE_INPUT] ); 

    } 
} 
return EXIT_SUCCESS; 
}
</code></pre>