Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The four threads will have the same PID but only when viewed from <em>above.</em> What <em>you</em> (as a user) call a PID is not what the kernel (looking from below) calls a PID.</p> <p>In the <em>kernel,</em> each thread has it's own ID, called a PID (although it would possibly make more sense to call this a TID, or thread ID) and they also have a TGID (thread group ID) which is the PID of the thread that started the whole process.</p> <p>Simplistically, when a new <em>process</em> is created, it appears as a thread where both the PID and TGID are the same (new) number.</p> <p>When a thread starts another <em>thread,</em> that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.</p> <p>That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.</p> <p>The following hierarchy of threads may help<sup>(a)</sup>:</p> <pre><code> USER VIEW &lt;-- PID 43 --&gt; &lt;----------------- PID 42 -----------------&gt; +---------+ | process | _| pid=42 |_ _/ | tgid=42 | \_ (new thread) _ _ (fork) _/ +---------+ \ / +---------+ +---------+ | process | | process | | pid=44 | | pid=43 | | tgid=42 | | tgid=43 | +---------+ +---------+ &lt;-- PID 43 --&gt; &lt;--------- PID 42 --------&gt; &lt;--- PID 44 ---&gt; KERNEL VIEW </code></pre> <p>You can see that starting a new <em>process</em> (on the left) gives you a new PID <em>and</em> a new TGID (both set to the same value), while starting a new <em>thread</em> (on the right) gives you a new PID while maintaining the same TGID as the thread that started it.</p> <hr> <p><sup>(a)</sup> <em>Tremble in awe</em> at my impressive graphical skills :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload