If threads share the same PID, how can they be identified?

SPSN picture SPSN · Feb 16, 2012 · Viewed 52.4k times · Source

I have a query related to the implementation of threads in Linux.

Linux does not have an explicit thread support. In userspace, we might use an thread library (like NPTL) for creating threads. Now if we use NPTL it supports 1:1 mapping.

The kernel will use the clone() function to implement threads.

Suppose I have created 4 threads. Then it would mean that:

  • There will be 4 task_struct.
  • Inside the task_struct, there will be provision of sharing resources as per the arguments to clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND).

Now I have the following query:

  1. Will the 4 threads have the same PID? If someone can elaborate, how the PIDs are shared.
  2. How are the different threads identified; is there some TID (thread ID) concept?

Answer

paxdiablo picture paxdiablo · Feb 16, 2012

The four threads will have the same PID but only when viewed from above. What you (as a user) call a PID is not what the kernel (looking from below) calls a PID.

In the kernel, 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.

Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.

When a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.

That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.

The following hierarchy of threads may help(a):

                      USER VIEW
 <-- PID 43 --> <----------------- PID 42 ----------------->
                     +---------+
                     | process |
                    _| pid=42  |_
                  _/ | tgid=42 | \_ (new thread) _
       _ (fork) _/   +---------+                  \
      /                                        +---------+
+---------+                                    | process |
| process |                                    | pid=44  |
| pid=43  |                                    | tgid=42 |
| tgid=43 |                                    +---------+
+---------+
 <-- PID 43 --> <--------- PID 42 --------> <--- PID 44 --->
                     KERNEL VIEW

You can see that starting a new process (on the left) gives you a new PID and a new TGID (both set to the same value), while starting a new thread (on the right) gives you a new PID while maintaining the same TGID as the thread that started it.


(a) Tremble in awe at my impressive graphical skills :-)