title: How to correctly receive data from pipes?
body: <p>So I wrote a program using pipes and execl to do some arithmetic. I'm not sure how to debug pipes and different processes, but from what I can see it has to have something to do with my pipes reading the numbers. So my setup so far is the children are created, and based on thier given operation, they execl to different programs that will calculate the written values in the pipe. I don't know if I should implement a wait or something in the parent after it has written its values to the children before it reads the result. I keep getting Z as my output.</p>
  <p>So this is one of the files I'm execl to and the other files look exactly the same it just shows its corresponding operation:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  main(int argc, char *argv[])
  {
      int x, y, z;
      while (read(0, (char *)&amp;x, sizeof(int)) &amp;&amp;
             read(3, (char *)&amp;y, sizeof(int))) {
          z = x * y;
          if (argc &gt; 1)
              cerr &lt;&lt; &quot;multiply: &quot; &lt;&lt; x &lt;&lt; &quot; * &quot; &lt;&lt; y &lt;&lt; &quot; = &quot; &lt;&lt; z &lt;&lt; endl;
          write(1, (char *)&amp;z, sizeof(int));
      }
  }
  </code></pre>
  <p>This is my code, which would actually be doing the calculating, which I think the pipes are set up correctly. Just a little explanation on the pipe setup, to save time. Child 0 -- reads from pipe 0 1 and writes to 2.  Child 1 reads from pipe 2 and 3 and writes to 4. The parent writes to 0 1 3  and reads from 4. My code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  const int MAX =21;
  int pipes[MAX][2];
  char operations[MAX];
  
  int main()
  {
      int a=0;
      int n;
      fstream datafile;
      pid_t pid;
      string line;
      datafile.open(&quot;data1.txt&quot;);
     
  
     if(datafile)
      {
  
          string input;
          getline(datafile, input);
          
          for(int i=0; i&lt; input.size(); i++)
          {
  
      
              if (input[i] == '*'|| input[i] == '/'||input[i] == '+'||input[i] == '-')
              {
        
                  operations[a] = input[i];
                  a++;
  
              }
  
          }
        n = (a);
  
          for(int i =0; i&lt;(2*n+1); i++)
             pipe(pipes[i]);
  
          for (int i=0; i &lt;n; i++)
          {    
             pid = fork();
              if(pid == 0)
              {
               
                  close(0);
                  dup(pipes[2*i][0]);
                  close(3);
                  dup(pipes[2*i+1][0]);
                  close(1);
                  dup(pipes[2*i+2][1]);
                
              
                      switch(operations[i])
                      {
                      case '+':
                          execl(&quot;add&quot;,&quot;add&quot;, NULL);
                      case '-': 
                          execl(&quot;subtract&quot;,&quot;multiply&quot;, NULL);
                      case '*':
                          execl(&quot;multiply&quot;,&quot;multiply&quot;, NULL);
                      case '/':
                          execl(&quot;divide&quot;,&quot;divide&quot;, NULL);
                      
                      }
  
                   cout&lt;&lt; &quot;No match for operation!&quot;;
              }
              else if(pid &lt;0)
              cerr&lt;&lt;&quot;Fork has failed&quot;;
            }
      
          int x, y, z;
  
          for(int i=0; i&lt;3; i++)
            {
              getline(datafile,line);
              istringstream ins(line);
              ins &gt;&gt; x &gt;&gt; y &gt;&gt; z;
  
              write(0,(char *)&amp;x, sizeof(x));
              write(3,(char *)&amp;z, sizeof(z));
              write(1,(char *)&amp;y, sizeof(y));
            }
            
            close(0);
            close(3);
            close(1);
          }  
          
              else
                  cerr&lt;&lt;&quot;Error reading file&quot;;
            
  
  
            datafile.close();
            
              int result;
              while(read(pipes[2*n][1],(char *)&amp;result,sizeof(result)))
                  cout&lt;&lt;result&lt;&lt;endl;
      
          
          
   
          
  
  
  
  }
  </code></pre>
  <p>datafile can be this:</p>
  <blockquote>
  <p>a * b *c</p>
  <p>30 40 50</p>
  <p>100 3 6</p>
  </blockquote>