title: The relationship between thread and process in multi-process program
body: <p>OS: debian9.<br>
  A simple multi-processes program named <code>mprocesses.py</code>.</p>
  
  <pre><code>import os
  import multiprocessing
  
  def run_task(name):
      print("task %s (pid = %s) is running"  %(name,os.getpid()))
      while True:
          pass
  
  if __name__ == "__main__":
      print("current process %s ." %os.getpid())
      pool = multiprocessing.Pool(processes = 2)
      for i in range(2):
          pool.apply_async(run_task,args=(i,))
      pool.close()
      pool.join()
  </code></pre>
  
  <p>Run <code>python3 mprocesses.py</code> and get below output.</p>
  
  <pre><code>python3 mprocesses.py
  current process 6145 .
  task 0 (pid = 6146) is running
  task 1 (pid = 6147) is running
  </code></pre>
  
  <p>Get processes info.</p>
  
  <pre><code>ps lax |grep 'python3 mprocesses.py' |grep -v grep 
  0  1000  6145  5615  20   0 275428 14600 -      Sl+  pts/1      0:00 python3 mprocesses.py
  1  1000  6146  6145  20   0  54232 10340 -      R+   pts/1      1:01 python3 mprocesses.py
  1  1000  6147  6145  20   0  54232 10348 -      R+   pts/1      1:01 python3 mprocesses.py
  </code></pre>
  
  <p>Check processes tree view.</p>
  
  <pre><code>pstree -p 5615
  bash(5615)───python3(6145)─┬─python3(6146)
                             ├─python3(6147)
                             ├─{python3}(6148)
                             ├─{python3}(6149)
                             └─{python3}(6150)
  </code></pre>
  
  <p>What confused me is the three threads 6148,6149,6150.<br>
  Does that mean every process contain one process ? 
  Maybe my logical graph is better to express relationships between processes and threads here.</p>
  
  <pre><code>bash(5615)───python3(6145)─┬─────────────────python3(6146)
                             |                    └─{python3}(6149)
                             |             
                             ├──────────────────python3(6147)
                             ├─{python3}(6148)     └─{python3}(6150)
  </code></pre>
  
  <p>1.bash(5615) is the <code>python3 mprocesses.py</code>(6145) 's father process.<br>
  2.<code>python3 mprocesses.py</code>(6145)  contains two processes 6146 and 6147 created by <code>pool = multiprocessing.Pool(processes = 2)</code>.<br>
  3.Process(6145) contain thread(6148),Process(6146) contain thread(6149),Process(6147) contain thread(6150).<br>
  It does no matter which exact process id contain which thread id.<br>
  Is my understanding right?    </p>