title: How does fork() know when to return 0?
body: <p>Take the following example:</p>
  
  <pre><code>int main(void)
  {
       pid_t  pid;
  
       pid = fork();
       if (pid == 0) 
            ChildProcess();
       else 
            ParentProcess();
  }
  </code></pre>
  
  <p>So correct me if I am wrong, once fork() executes a child process is created. Now going by this <a href="https://stackoverflow.com/a/2562743/5826291">answer</a> fork() returns twice. That is once for the parent process and once for the child process. </p>
  
  <p>Which means that two separate processes come into existence DURING the fork call and not after it ending. </p>
  
  <p>Now I don't get it how it understands how to return 0 for the child process and the correct PID for the parent process. </p>
  
  <p>This where it gets really confusing. This <a href="https://stackoverflow.com/a/23819945/5826291">answer</a> states that fork() works by copying the context information of the process and manually setting the return value to 0. </p>
  
  <p>First am I right in saying that the return to any function is placed in a single register? <strong>Since in a single processor environment a process can call only one subroutine that returns only one value (correct me if I am wrong here).</strong> </p>
  
  <p><strong>Let's say I call a function foo() inside a routine and that function returns a value, that value will be stored in a register say BAR. Each time a function wants to return a value it will use a particular processor register.</strong> So if I am able to manually change the return value in the process block I am able to change the value returned to the function right?</p>
  
  <p><strong><em>So am I correct in thinking that is how fork() works?</em></strong></p>