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

I have a request related to thread implementation on Linux.

Linux does not support explicit threads. In user space, we can use a thread library (e.g. NPTL) to create threads. Now, if we use NPTL, it supports 1: 1 mapping.

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

Suppose I created 4 threads. Then this will mean that:

  • There will be 4 task_struct .
  • Inside task_struct will be separated by cloning arguments (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND) .

Now I have the following query:

  • Will 4 threads have the same PID? If someone can clarify how the PIDs are distributed.
  • How different streams are identified; Is there a concept for a TID (thread ID)?
+58
multithreading linux-kernel pid
Feb 16 '12 at 5:23
source share
3 answers

Four streams will have the same PID, but only when viewed from above. The fact that you (as a user) call PID is not that the kernel (looking from below) calls PID.

In the kernel, each thread has its own identifier called a PID (although it might make sense to call it a TID or a thread identifier), and they also have a TGID (thread group identifier), which is the PID of the thread that started the whole process.

Simplified, when a new process is created, it is displayed as a thread, where the PID and TGID are the same (new).

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

Thus, the kernel can happily schedule threads regardless of which process they belong to, while processes (thread group identifiers) are informed to you.

The following thread hierarchy can help 1 :

  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 gives you a new PID and a new TGID (both are set to the same value), and starting a new thread gives you a new PID while maintaining the same TGID as the thread that started it.




1 Tremble with admiration for your impressive graphic skills :-)

+169
Feb 16 2018-12-12T00:
source share

Streams are identified using PID and TGID (thread group identifier). They also know which thread is the parent of the one who essentially processes his PID with any threads that he starts. The thread ID is usually managed by the thread library itself (e.g. pthread, etc.). If 4 threads are running, they must have the same PID. The kernel itself will handle threads and such, but the library is the one that will manage the threads (regardless of whether they can be executed or not, depending on the use of methods for combining threads and waiting).

Note. This is from my memory of the 2.6.36 kernel. My work in current kernel versions is at the I / O level, so I don't know if this has changed since then.

+1
Feb 16 2018-12-12T00:
source share

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

-5
Oct. 20 '13 at 4:47
source share



All Articles