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 :-)
paxdiablo Feb 16 2018-12-12T00: 00Z
source share