If threads share the same PID, how can they be identified?
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:
- Will the 4 threads have the same PID? If someone can elaborate, how the PIDs are shared.
- How are the different threads identified; is there some TID (thread ID) concept?
Solution 1:
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 :-)
Solution 2:
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.