title: synchronizing 2 processes
body: <p>I'm trying to make 2 processes start on a task at the same time (count a number, for example). I set 2 ready flags, one for each process, and perform a while loop to check if both flags are up. Then the 2 processes will start counting after the check is passed. Here's the not working code, which I don't know why:</p>
  
  <pre><code>int p1ready=0;
  int p2ready=0;
  int onebil = 1000000000;
  
  int main(){
  
  int pid;
  int exit_code;
  
  pid=fork();
  if(pid==0){
  //child1
      int count1=0;
      p1ready=1;    //signal
      while(!(p1ready&amp;p2ready))'//wait until 2 processes are both ready
  
      while(count1!=onebil){
          count1++;
      }
      exit(0);
  }
  else{
      pid=fork();
      if(pid==0){
      //child2
          int count2=0;
          p2ready=1;    //signal
          while(!(p1ready&amp;p2ready));//wait until 2 processes are both ready
          while(count2!=onebil){
              count2++;
              }
          exit(0);
      }
      else{
      //parent
         //do stuff
      }
  return 0;
  }
  </code></pre>
  
  <p>The problem with this code is, in child1 and child2, only their own ready flag is set to 1. They cannot see the flag of the other child being set. For example, child1 only sees p1ready=1, but p2ready is always 0. Why is this so? How can I fix this? </p>
  
  <p>Thanks in advance!</p>