Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the thread ID of a multithreaded process be the same as the process ID of another running process?

I'm trying to find a way to uniquely identify threads in a multi-process environment. I have a server that keeps track of the different processes connecting to it, some of which are multi-threaded and some of which are not. To identify the threads from multi-threaded connections I'm using the thread ID as a unique identifier (there will be a maximum of 1 multi-threaded process connected at any given time). My question is: is it possible the thread ID of one of these threads could be the same as the process ID of another processes running on the system?

Thanks in advance for the help!

like image 998
mgcleveland Avatar asked Jan 02 '13 16:01

mgcleveland


People also ask

Is thread ID same as process ID?

The thread ID of the initial (main) thread is the same as the process ID of the entire process. Thread IDs for subsequently created threads are distinct. They are allocated from the same numbering space as process IDs. Process IDs and thread IDs are sometimes also referred to collectively as task IDs.

Do all threads have the same process ID?

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.

Do two threads have the same PID?

multiple threads can share the same pid . The kernel, however, views them as tid , since NPTL is implemented in user space, the kernel doesn't really know much about NPTL threads, but threads created ( clone with CLONE_THREAD flag) in the same process share the same pid .

Do threads share thread ID?

Thread IDs can be recycled. Your code is starting a thread and waiting for it to finish. There are never more then one (apart from the main-thread) running. Hence, you get the same ID.


1 Answers

The TID (as returned by the sys_gettid() system call) is unique across all threads on the system1, and for a single-threaded process the PID and TID are equal. This means that a TID will never clash with a PID from another process.


1. With the caveat that if PID namespaces are in use, TIDs and PIDs are only unique within the same PID namespace.
like image 140
caf Avatar answered Oct 07 '22 22:10

caf