title: May someone explain the following os.fork() example to me?
body: <p>[Code taken from Programming Python 4th Edition by Mark Lutz]</p>
  
  <pre><code>"forks child processes until you type 'q'"
  import os
  def child():
      print('Hello from child', os.getpid())
      os._exit(0) # else goes back to parent loop
  
  def parent():
      while True:
          newpid = os.fork()
          if newpid == 0:
              child()
          else:
              print('Hello from parent', os.getpid(), newpid)
          if input() == 'q': break
  
  parent()
  </code></pre>
  
  <p>What the code outputs when ran:</p>
  
  <pre><code>Hello from parent 2057 2062 
  Hello from child 2062
  
  Hello from parent 2057 2068 
  Hello from child 2068
  
  Hello from parent 2057 2069 
  Hello from child 2069
  
  Hello from parent 2057 2070 
  Hello from child 2070 
  q
  </code></pre>
  
  <p>Things I understand:</p>
  
  <ol>
  <li><code>os.fork()</code> is used to start another process in parallel to the current one.</li>
  <li><code>os.fork()</code> creates a copy of the previous Python session and opens it in parallel.</li>
  <li><code>os.fork()</code> returns the id of the new process.</li>
  </ol>
  
  <p>Things I don't understand:</p>
  
  <ol>
  <li>How come the value of <code>os.getpid()</code> is never changed when the code runs?</li>
  <li>Why is the <code>child()</code> function ever called? Let's say that the value of <code>newpid</code> != 0, then the program will print out <code>print('Hello from parent', os.getpid(), newpid)</code>. However, after that, it prints the line from child rather than asking for an input as the case is after the if statement.</li>
  <li>What is <code>os._exit(0)</code> doing?</li>
  </ol>
  
  <p>Thanks a ton for your time. :)</p>