title: Segments duplicated during fork()?
body: <p>I have studied that during a fork, the data and code segment of the parent process gets duplicated into the child process.</p>
  
  <p>Kindly see the program below.</p>
  
  <pre><code>int main()
  {
     int a = 5;
     pid_t pid;
  
     pid = fork();
  
     if(pid == 0)
     {
        printf("In child a = %d",a);
     }
  
     else
     {
       printf("In parent a = %d",a);
     }
  
     return 0;
  }
  </code></pre>
  
  <p>Here a is in the stack segment of parent process as it is declared inside the function, <code>main()</code>. The child process should only get copy of the code and data segment of the parent process and not the stack during <code>fork()</code>. But when I run the program, I can see that the child process is able to access the variable 'a' also. Thats means somehow the stack of parent process is also copied into the child process.</p>
  
  <p>Kindly tell me the reason for this and correct me if my understanding is wrong.</p>