title: parent local variable acting as shared variable between three child
body: <p>How come in the below program the local variable of parent process is acting as a shared variable between three child processes.</p>
  
  <pre><code>int main()
  {
      int turn = 0;
      int i;
  
      for (i = 0; i &lt; 3; i++)
      {
          if (fork() == 0)
          {
              int me = i;
              while (turn != me)
                  /*do nothing*/ ;
  
              // my turn
              printf("Process %d ran\n", me);
              turn++;
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <p>Process 0 ran</p>
  
  <p>Process 1 ran</p>
  
  <p>Process 2 ran</p>
  
  <p>But according to me the last two processes should hang as the value of turn should never change for them.</p>
  
  <p>Also if i put a exit(0); after turn++, I am returned immediately to my shell prompt with only one line of output i.e:
  Process 0 ran</p>
  
  <p>But still the other two processes keep running in the background and no output by any of the other two processes.</p>