title: Sorting with shared memory with child processes
body: <p>I'm trying to use child processes in my quicksort, such that the left half sorts in one child and the right half in another child. I had it working prior to the shmget implementation but now I believe I'm corrupting the array somewhere because all my values become zero after printing the array. Sorry if I'm making some dumb mistake somewhere, I'm trying to learn how to use fork and shmget but have been running into some trouble. I'm trying to take in a text file as a command line argument and given a delimiter such as ';' I have to remove the delimiter and identify the numbers in between, place them in an array and sort them using child processes. I have the parsing working, and the quicksort worked but now that I'm trying to implement the shared memory I've ran into some problems.</p>
  
  <p>Thanks</p>
  
  <p>I've looked at a few different examples, but the one that this is mostly based off is the geeksforgeeks example with mergesorting with fork.
  <a href="https://www.geeksforgeeks.org/concurrent-merge-sort-in-shared-memory/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/concurrent-merge-sort-in-shared-memory/</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;memory.h&gt;
  #include "fileParser.h"
  #include "dataManagement.h"
  
  int main(int argc, char *argv[]){
      char *file = argv[1];
      char delimiter = argv[2][0];
      MyArray *theArray = getArray(file, delimiter);
  
      size_t SHM_SIZE = theArray-&gt;length;
  
      theArray-&gt;key = IPC_PRIVATE;
  
  
      if((theArray-&gt;shmid = shmget(theArray-&gt;key, SHM_SIZE, IPC_CREAT | 0666)) &lt; 0){
          perror("shmget");
          _exit(-1);
      }
  
      if ((theArray-&gt;shm_array = shmat(theArray-&gt;shmid, NULL, 0)) == (int *) -1)
      {
          perror("shmat");
          _exit(1);
      }
  
      printArray(theArray, theArray-&gt;length);
      quickSortFork(theArray, 0, theArray-&gt;length-1);
      printArray(theArray, theArray-&gt;length);
  
      if (shmdt(theArray-&gt;shm_array) == -1)
      {
          perror("shmdt");
          _exit(1);
      }
  
      if (shmctl(theArray-&gt;shmid, IPC_RMID, NULL) == -1)
      {
          perror("shmctl");
          _exit(1);
      }
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>dataManagement.h</p>
  
  <hr>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include "fileParser.h"
  
  int partition(MyArray *arr, int low, int high);
  void quickSortFork(MyArray *arr, int low, int high);
  void swap(MyArray *arr, int a, int b);
  
  
  void printArray(MyArray *arr, int length) {
      for(int i = 0; i &lt; length; i++){
          printf("%d  ", arr-&gt;shm_array[i]);
      }
      printf("\n");
  }
  
  
  void quickSortFork(MyArray *arr, int low, int high){
      pid_t lpid, rpid;
      rpid = fork();
      if(low &lt; high){
          int partitionIndex = partition(arr,low, high);
          if(rpid &lt; 0){
              perror("Right child not created.\n");
              exit(-1);
          } else if(rpid == 0 ){
              printf("I am the right child!\tMy process id: %d\n",getpid());
              quickSortFork(arr, partitionIndex + 1, high);
              exit(EXIT_SUCCESS);
          } else {
              lpid = fork();
              if(lpid &lt; 0){
                  perror("Left child not created.\n");
              } else if (lpid == 0) {
                  quickSortFork(arr, low, partitionIndex - 1);
                  printf("I am the left child!\tMy process id: %d\n", getpid());
                  exit(EXIT_SUCCESS);
              }
          }
  
          int status;
  
          waitpid(rpid, &amp;status, 0);
          waitpid(lpid, &amp;status, 0);
      }
  }
  
  
  int partition(MyArray *arr, int low, int high){
      int i = low, j = high;
      int pivot = arr-&gt;shm_array[(low+high)/2];
      while(i &lt; j){
          while(arr-&gt;shm_array[i] &lt; pivot)
              i++;
          while(arr-&gt;shm_array[j] &gt; pivot)
              j--;
          if(i &lt; j){
              swap(arr,i,j);
          }
      }
      return i;
  }
  
  
  void swap(MyArray *arr, int a, int b){
      int temp = arr-&gt;shm_array[a];
      arr-&gt;shm_array[a] = arr-&gt;shm_array[b];
      arr-&gt;shm_array[b] = temp;
  }
  </code></pre>
  
  <hr>
  
  <p>fileParser.h</p>
  
  <hr>
  
  <pre><code>int findFileLength(FILE *inFile, char delimiter);
  int *parseFileToArray(FILE *inFile, char delimiter, int length);
  
  typedef struct {
      int shmid;
      key_t key;
      int length;
      int *shm_array;
  } MyArray;
  
  
  MyArray *getArray(char *fileName, char delimiter){
      FILE *numberFile = fopen(fileName, "r"); // open for reading
  
      if (numberFile == NULL) // unable to open file
          return NULL;
      MyArray *array = malloc(sizeof(MyArray));
      array-&gt;length = findFileLength(numberFile, delimiter);
      array-&gt;shm_array = parseFileToArray(numberFile, delimiter,array-&gt;length);
  
      return array;
  
  }
  
  
  int findFileLength(FILE *inFile, char delimiter){
      char c;
      int length = 0;
      c = fgetc(inFile);
      while(c != EOF){
          if(c != delimiter &amp;&amp; c != '\n'){
              length++;
              while((c = fgetc(inFile)) != EOF &amp;&amp; c != '\n' &amp;&amp; c != delimiter);
  
          } else {
              c = fgetc(inFile);
          }
      }
      rewind(inFile); // resets the file pointer to the start
      return length;
  }
  
  
  int *parseFileToArray(FILE *inFile, char delimiter, int length){
      int *parsedFile = malloc(sizeof(int) * length);
      char c;
      char *stringInt = malloc(sizeof(char) * 100); // string that is used to combine multiple characters and convert to an integer
      int stringIntP = 0, parsedArrayP = 0; // pointers for our arrays, the first is for the string that determines the integer, the second is for our resulting array
      c = fgetc(inFile);
      while(c != EOF){
          if(c != delimiter &amp;&amp; c != '\n'){
  
              for(;c != '\n' &amp;&amp; c != delimiter; (c = fgetc(inFile)) != EOF){
                  stringInt[stringIntP++] = c;
              }
              stringIntP = 0;
              parsedFile[parsedArrayP++] = atoi(stringInt); // convert string number to integer value
              memset(stringInt, 0, 100);  // clear the string that builds the integer value from chars
          } else {
              c = fgetc(inFile);
          }
      }
      for(int i = 0; i &lt; length; i++){
          printf("%d ", parsedFile[i]);
      }
      printf("\n");
      fclose(inFile); // close the file after using
      free(stringInt);
      return parsedFile;
  }
  </code></pre>
  
  <p>Expected output: the array passed in unsorted first. Then the array sorted.</p>
  
  <p>Actual output: array filled with 0's and program doesn't finish executing</p>