Can The Thread Id of a Multithreaded Process Be The Same as The Process Id of Another Running Process

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

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.

Relation between Thread ID and Process ID

I got the answer here on stackoverflow. It states that if we run a program on Linux that contains the libc libuClibc-0.9.30.1.so (1). Basically an older version of libc then thread created will have different PID as shown below

root@OpenWrt:~# ./test
main thread pid is 1151
child thread pid is 1153

and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2) i.e newer version of libc then Thread created will have the same PID as the process.

$ ./test
main thread pid is 2609
child thread pid is 2609
The libc (1) use linuxthreads implementation of pthread

And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

According to the linuxthreads FAQ (in J.3 answer):

each thread is really a distinct process with a distinct PID, and signals sent to the PID of a thread can only be handled by that thread

So in the old libc which use linuxthreads implementation, each thread has its distinct PID

In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

(Chapter: Problems with the Existing Implementation, page5)

Each thread having a different process ID causes compatibility problems with other POSIX thread implementations. This is in part a moot point since signals can't be used very well but is still noticeable

And that explain this issue.

I am using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread.

Can the same thread ID be reused within the same process?

The documentation for pthread (which both std::thread and boost::thread use) says,

Thread IDs are guaranteed to be unique only within a process. A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

http://man7.org/linux/man-pages/man3/pthread_self.3.html

So, yes, a new thread may reuse an ID from a dead thread inside the same process.

Does the thread id change after forking a new process?

If you read the pthread_self manual page you will see that

Thread IDs are guaranteed to be unique only within a process.

(Emphasis mine)

That of course means that two very different processes may have threads with the same id.

If you for some reason want to get the unique kernel id of the thread, use gettid instead.

Are thread IDs unique per process or per computer?

Yes, thread ID's and Process ID's on Windows are allocated from the same pool, so they will be unique. Once the thread or process ends however, the ID may be re-used by another thread or process.

Is the python threading.ident the same as the Linux process id?

The threading.ident property provides the thread identity, which is only used to distinguish between other threads or whether a thread has started.

If you want the process id of your application, you can use os.getpid() to get the PID of your current process. Alternatively, if you are using subprocess or multiprocessing to call your sub-processes, you can access their pids using subprocess.Popen().pid or multiprocessing.Process().pid respectively.

subprocessing PID documentation

multiprocessing PID documentation

How can multiple threads have the same thread ID?

for(int i=0;i<10;i++){
report[i] = pthread_create(&threads[i],NULL,message,(void*)i);
pthread_join(threads[i],NULL);
}

You're creating a thread and then immediately joining it, waiting for it to finish. This causes the ten threads to be created sequentially rather than in parallel, allowing their IDs to be recycled.

If you create all ten threads before joining on them you'll see different IDs.

for(int i=0;i<10;i++){
report[i] = pthread_create(&threads[i],NULL,message,(void*)i);
}

for(int i=0;i<10;i++){
pthread_join(threads[i],NULL);
}

How do you differentiate between the process and thread in python?

I'll answer your questions directly:

From what I understand, thread and process differs in that thread shares resources.

This is not true, at least not from your understanding. Consider that you can edit a file in Notepad and in WordPad at the same time; both are different processes sharing the same resource; a file in this case.

Conceptually, a process is a single "application" that shares the resources of the operating system and other processes while a thread is a single "execution context" that shares the same basic resources of a process.

It's a little more complicated than that, but at a high level that's the basic difference between a process and a thread.

I am not sure if this differs from one language to another, but how do you differentiate between thread and process in general or in Python?

Given the prior point, in a general basic context, a thread is owned by a process, and every process has at least one thread.

From a code perspective, you can differentiate by getting the process ID and thread ID, and that does depend on your langauge.

For Python, you would import os and then call os.getpid() to get the current process ID, and if you're using Python 3.8 you can call threading.get_native_id() to get the thread id from which the function is currently executed on.

If you were to create 10 threads within the same process, each thread should return the same value for os.getpid() but should return 10 different ID's for threading.get_native_id().

Additionally, a thread within a process can spawn another thread or separate process. For example, a single process (read: application) could create 10 threads, and in each thread could spawn an external application (read: process), but that process is not owned by the thread that spawned it like the thread is owned by the process that spawned it.

Another basic way to think of it, all processes are children of the OS, and all threads are children of the parent process, no matter which thread or process created what.

Are every independent functions different process? Would a class methods be threads as they share memory? would recursive functions be thread?

No. Each function lives within the same process memory space, including classes and their member functions. Recursive functions are neither processes nor threads, they are functions that call themselves.

You could create a thread that calls a specific function, and that function could potentially be recursive, but again, you have to have your code specifically create a thread and utilize the function address which lives within the process created when your init code was called (main in other languages).

If threading or processing module is not used, how can it be differentiated?

Referring again to the previous points, you differentiate it by understanding what a process is and what a thread is.

and IS there a need to differentiate them?

Yes. Conceptually they are different things, and once you actually start doing various types of multi-threaded, multi-process, memory-mapped code, you do need to understand that difference and how it pertains to your code.

At a bare minimum, if you never write a single line of multi-threaded code in your career, it is important to at least understand how they relate.



Related Topics



Leave a reply



Submit