title: Reading and Writing with Multiple Children C++ Unix
body: <p>So, this is a follow up on my assignment I posted earlier. 
  Link: <a href="https://stackoverflow.com/questions/42913617/piping-and-forking-in-c-in-unix">https://stackoverflow.com/questions/42913617/piping-and-forking-in-c-in-unix</a></p>
  
  <p>Got that section of the code all working now, but now I have to move onto 4 processes, which is giving me a hard time. Just as a recap, I have to take data files and have the sum of the integers added up using piping and forking. Currently I am having problems getting the sum to come up, if I put int mypipe[4] and change all my variables accordingly, my program freezes up. Currently trying two pipes, but only will show the sum of one of the pipes. Here is my code I have for the 4 process portion: </p>
  
  <pre><code>else
      {
              pid_t child1, child2, child3, child4;
              int mypipe1[2];
              int mypipe2[2];
  
              //File 1
              t1=clock();
              int temptotal=0;
              int file1[1000];
              for(int i = 0; i &lt; 1000; i++)
              {
                      infile1 &gt;&gt; file1[i];
              }
  
              pipe(mypipe1);
              pipe(mypipe2);
              //Begin the forking
              if((child1 = fork()) == 0)
              {
                      close(mypipe1[1]);
                      close(mypipe2[0]);
                      close(mypipe2[1]);
                      for(int i = 0; i &lt; 250; i++)
                      {
                              temptotal = temptotal + file1[i];
                      }
                      write(mypipe1[0], &amp;temptotal, sizeof(temptotal));
                      temptotal=0;
                      exit(0);
              }
              else
              {
                      if((child2 =fork())==0)
                      {
                              close(mypipe1[0]);
                              close(mypipe2[1]);
                              close(mypipe2[0]);
                              for(int i =250; i &lt; 500; i++)
                              {
                                      temptotal = temptotal + file1[i];
                              }
                              write(mypipe1[1], &amp;temptotal, sizeof(temptotal));
                              temptotal=0;
                              exit(1);
                      }
                      else
                      {
                              if((child3=fork())==0)
                              {
                                      close(mypipe1[0]);
                                      close(mypipe1[1]);
                                      close(mypipe2[1]);
                                      for(int i =500; i &lt; 750; i++)
                                      {
                                              temptotal = temptotal + file1[i];
                                      }
                                      write(mypipe2[0], &amp;temptotal, sizeof(temptotal));
                                      temptotal=0;
  
                      exit(0);
                              }
                              else
                              {
                                      if((child4=fork())==0)
                                      {
                                              close(mypipe1[0]);
                                              close(mypipe1[1]);
                                              close(mypipe2[0]);
                                              for(int i =750; i &lt; 1000; i++)
                                              {
                                                      temptotal = temptotal + file1[i];
                                              }
                                              write(mypipe2[1], &amp;temptotal, sizeof(temptotal));
                                              temptotal=0;
                                              exit(1);
                                      }
                                      else
                                      {
                                              //Parent Proccess
                                              int n1,n2,n3,n4;
                                              read(mypipe1[0],&amp;n1,sizeof(n1));
                                              close(mypipe1[0]);
                                              read(mypipe1[1],&amp;n2,sizeof(n2));
                                              close(mypipe1[1]);
                                              read(mypipe2[0],&amp;n3,sizeof(n3));
                                              close(mypipe2[0]);
                                              read(mypipe2[1],&amp;n4,sizeof(n4));
                                              close(mypipe2[1]);
  
                                              total = n1+n2+n3+n4;
                                              std::cout&lt;&lt;"File1 sum: "&lt;&lt;total&lt;&lt;std::endl;
                                      }
                              }
                      }
              }
      }
  </code></pre>
  
  <p>Any ideas what I am missing? Any input is greatly appreciated! </p>