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

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


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

In the kernel, each thread has its 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 first thread that was created when the process was created.

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

When a thread starts another thread, that new 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                         vvvv vvvv              |          <-- PID 43 -->|<----------------- PID 42 ----------------->              |                           |              |      +---------+          |              |      | process |          |              |     _| pid=42  |_         |         __(fork) _/ | tgid=42 | \_ (new thread) _        /     |      +---------+          |       \+---------+   |                           |    +---------+| process |   |                           |    | process || pid=43  |   |                           |    | pid=44  || tgid=43 |   |                           |    | tgid=42 |+---------+   |                           |    +---------+              |                           |<-- 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). 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 :-)


Threads are identified using PIDs and TGID (Thread group id). They also know which thread is a parent of who so essentially a process shares its PID with any threads it starts. Thread ID's are usually managed by the thread library itself (such as pthread, etc...). If the 4 threads are started they should have the same PID. The kernel itself will handle thread scheduling and such but the library is the one that is going to be managing the threads (whether they can run or not depending on your use of thread join and wait methods).

Note: This is from my recollection of kernel 2.6.36. My work in current kernel versions is in the I/O layer so I don't know if that has changed since then.


Linux provide the fork() system call with the traditional functionality of duplicating a process. Linux also provides the ability to create threads using the clone() system call However , linux does not distinguish between processes and thread.