title: Pipe a struct from multiple children in C
body: <h2>Background:</h2>
  <p>I am doing a school assignment with the task of subsampling an image and using processes to simulate &quot;servers&quot; doing the calculations of certain pixels in the image. I'm very close to getting the assignment to work but I'm having a weird bug with reading and writing a struct with a pipe from multiple children to the parent.</p>
  <p>To illustrate this I tried to do a much simpler example without all the image subsampling stuff. I found that I was not able to get this very simple code to print the numbers 1-50 so maybe there is a bug in this code that I'm just not realizing or understanding properly.</p>
  <hr />
  <h2>Code:</h2>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  // Create a struct to hold the index
  struct Thing {
      int index;
  };
  
  int main() {
  
      pid_t pid;
      int processLabel;
      int c2p[2];
      int p = 2;
  
      // create p number child processes
      for(processLabel = 0; processLabel &lt; p; processLabel++)
      {
          if(pipe(c2p) == -1)
          {
              perror( &quot;pipe Failed&quot; );
              continue;
          }
  
          pid = fork();
  
          if(pid  == 0) {
              break;
          }
      }
  
      if (pid == 0) { // child process
          int count = 0;
  
          // close read from parent process
          close(c2p[0]);
  
          for(int i = 0; i &lt; 50; i++){
              // write the index to parent depending on what child process we are in
              if(count % p == processLabel){
                  struct Thing test;
                  test.index = count;
                  write(c2p[1], &amp;test, sizeof(test));
              }
              count++;
          }
          close(c2p[1]);
      }
      else { // parent
          close(c2p[1]);
          
          // read structs from child processes
          for(int i = 0; i &lt; 50; i++){
              struct Thing test;
              read(c2p[0], &amp;test, sizeof(test));
              printf(&quot;index: %d\n&quot;, test.index);
          }
  
          close(c2p[0]);
      }
  
      return 0;
  }
  </code></pre>
  <p>The purpose of <code>if(count % p == processLabel)</code> is in my actual code for the assignment the child processes each do calculations on certain parts of the image. It is to &quot;simulate&quot; a server doing calculations of different parts of image to speed up the image subsampling.</p>
  <hr />
  <h2>Output:</h2>
  <p>The output that I receive from this code when <code>p</code> is 2 is the following:</p>
  <pre><code>index: 1
  index: 3
  index: 5
  index: 7
  index: 9
  index: 11
  index: 13
  index: 15
  index: 17
  index: 19
  index: 21
  index: 23
  index: 25
  index: 27
  index: 29
  index: 31
  index: 33
  index: 35
  index: 37
  index: 39
  index: 41
  index: 43
  index: 45
  index: 47
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  index: 49
  </code></pre>
  <p>As you can see it only prints out the odd numbers in the parent when I want it to print out 0 to 49. The interesting thing is when I change our value of <code>p</code> number of processes to 1, the code works properly and counts from 0-49.</p>
  <p>Thanks for any help on this!</p>