title: Using shared memory with fork()
body: <p>I already looked at the only similar post I could find, but it wasn't what I was looking for.</p>
  
  <p>Basically, I'm trying to run the <a href="http://en.wikipedia.org/wiki/Odd-even_sort" rel="nofollow">Odd-Even Sort</a> with forking, so the child runs odds and parent runs the evens. These both require the sharing of the vector inputValues, as well as the boolean sorted.</p>
  
  <p>The following code is without any of my failed attempts at sharing memory, and is just the basic framework for using forks with the search algorithm:</p>
  
  <pre><code>while(!sorted)
  {
      pID = fork();
      sorted = true;
      cout &lt;&lt; "Sort set to TRUE." &lt;&lt; endl;
  
      if(pID == 0)
      {
          int num = 1;
          cout &lt;&lt; "Child swap run" &lt;&lt; endl;
          Swap((void *) num);
          cout &lt;&lt; "Status: " &lt;&lt; ((sorted) ? "SORTED" : "UNSORTED") &lt;&lt; endl;
          exit(0);
      }
      else if(pID &lt; 0)
      {
          cout &lt;&lt; "Failed to fork." &lt;&lt; endl;
          exit(1);
      }
      else
      {
          wpid = waitpid(pID, &amp;status, waitStatus);
          int num = 0;
          cout &lt;&lt; "Parent swap run" &lt;&lt; endl;
          Swap((void *) num);
          cout &lt;&lt; "Status: " &lt;&lt; ((sorted) ? "SORTED" : "UNSORTED") &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>I've tried multiple ways of hacking out this sharing of memory, but can't find any one resource that really explains HOW it works, what I need, and the best way to do it.</p>
  
  <p>So, my requirements are as follows:</p>
  
  <ul>
  <li>The parent and child must be able to share and manipulate a global vector and boolean</li>
  <li>This must be able to run in a loop, as shown</li>
  <li>This must work with the variables being used in main() and in the swap() function</li>
  </ul>
  
  <p>If you have any tips, I'd greatly appreciate them. Thanks!</p>