I'm attempting to create a program that creates 2 child processes, and 4 pipes (I know this isn't ideal, but the spec for this specific assignment requires it). While it correctly sorts two of the 5 command line argument integers, the rest are just spat out as what I believe are uninitialized integers, I.E. 7 is printed out as 33234951.
I'm pretty new to pipes, and it's been a little hard to wrap my head around, so I believe this issue has to do with this and not some arbitrary error in code.
I was able to successfully get this done using only 1 parent and child, but as soon as I tried to implement multiple, things got dicey.
I have a lot of unused includes just from messing around with things in attempt to solve the problem.
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv) {
  printf("Starting\n");
  pid_t pid;
  pid_t pid2;
  int mypipe0[2];
  int mypipe1[2];
  int mypipe2[2];
  int mypipe3[2];
  pipe(mypipe0);
  pipe(mypipe1);
  pipe(mypipe2);
  pipe(mypipe3);
  /* Create the child process. */
  pid = fork();
  std::cout << "Fork " << pid << std::endl; 
  // Child: Sorts Array
  if (pid == 0) {
    printf("pid == (pid_t) 0 p2\n");
    /* This is the child process.
       Close other end first. */
    close(mypipe0[1]);
    char valuesArray[5];
    for (int a = 0; a < 5; a++)
      read(mypipe0[0], &valuesArray[a], sizeof(char));
    printf("finish reading mypipe0");
    std::sort(valuesArray, valuesArray + 5);
    printf("sorted");
    close(mypipe1[0]);
    close(mypipe2[0]);
    for (int a = 0; a < 5; a++) {
      write(mypipe1[1], &valuesArray[a], sizeof(char));
      write(mypipe2[1], &valuesArray[a], sizeof(char));
    }
    close(mypipe1[1]);
    close(mypipe2[1]);
    exit(0);
  } 
  else if (pid > 1) {
    std::cout << "pid == (pid_t) 1" << std::endl;
    /* This is the parent process.
        Close other end first. */
    close(mypipe0[0]); // Closes reading
    int valuesArray[5];
    valuesArray[0] = atoi(argv[1]);
    valuesArray[1] = atoi(argv[2]);
    valuesArray[2] = atoi(argv[3]);
    valuesArray[3] = atoi(argv[4]);
    valuesArray[4] = atoi(argv[5]);
    printf("Argv init");
    for (int a = 0; a < 5; ++a)
      write(mypipe0[1], &valuesArray[a], sizeof(char));
    printf("wrote to pipe 1");
    close(mypipe0[1]);
    wait(NULL);
    close(mypipe1[1]); // Closes writing
                       //            char outputArray[6];
    int sortedArray[5];
    for (int a = 0; a < 5; ++a)
      read(mypipe1[0], &sortedArray[a], sizeof(char));
    // Printing Array]
    for (int a = 1; a < 5; ++a)
      printf(", %d", sortedArray[a]);
    printf("]");
    //            wait(NULL);
    //            close(mypipe2[1]); // Closes writing
    //            int median;
    //            read(mypipe1[0], median, sizeof(charian));
    exit(0);
  }
  else {
    pid2 = fork();
    // Other child
    if (pid2 == 0) {
      printf("pid == (pid_t) 0\n");
      /* This is the child process.
         Close other end first. */
      close(mypipe0[1]);
      char valuesArray[5];
      for (int a = 0; a < 5; a++)
        read(mypipe0[0], &valuesArray[a], sizeof(char));
      printf("finish reading mypipe0");
      std::sort(valuesArray, valuesArray + 5);
      printf("sorted");
      close(mypipe1[0]);
      close(mypipe2[0]);
      for (int a = 0; a < 5; a++) {
        write(mypipe1[1], &valuesArray[a], sizeof(char));
        write(mypipe2[1], &valuesArray[a], sizeof(char));
      }
      close(mypipe1[1]);
      close(mypipe2[1]);
      exit(0);
    }
  }
}
I expect the output to be 2 4 5 6 7 from the given command line arguments of 4 2 5 6 7. Instead, I get [1, 28932, 5, 6, -14276913]
 
     
    