线程ID和进程ID之间的关系 [英] Relation between Thread ID and Process ID

查看:1105
本文介绍了线程ID和进程ID之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进程ID和线程ID之间有些困惑.我已经浏览了几篇网络文章,包括堆栈溢出此处,其中说

I am having some confusion between Process Id and Thread Id. I have gone through several web-post including stack overflow here, Which says

启动新进程会为您提供一个新的PID和一个新的TGID,而启动一个新线程会为您提供一个新的PID,同时保持相同的TGID.

starting a new process gives you a new PID and a new TGID, while starting a new thread gives you a new PID while maintaining the same TGID.

那么当我运行一个程序时,为什么从该程序创建的所有线程都没有不同的PID?

So when I run a program why all the threads created from the program don't have different PID?

我知道在编程中我们通常会说 main是一个线程,并且执行是从main开始的,因此,如果我从main创建多个线程,则所有线程将具有相同的PID,等于主电源的PID.

I know in programming we usually say that the main is a thread and execution starts from main , So if I create multiple thread from main, all the threads will have the same PID which is equal to the main's PID.

所以我想问的如下:

1)当我们运行程序时,它将作为进程或线程运行吗?

1) When we run a program it will run as a process or a thread?

2)线程创建线程和Process创建线程之间有什么区别吗?

2) Is there any difference between main thread creating threads and Process creating threads?

3)linux中的线程和进程之间有什么区别吗?由于我在某处读到linux在线程和进程之间没有区别.

3) Is there any difference between thread and process in linux? Since I read somewhere that linux doesn't differentiate between Thread and Process.

推荐答案

我在stackoverflow上的此处得到了答案.它指出如果我们在Linux上运行包含libc libuClibc-0.9.30.1.so(1)的程序.基本上,较旧版本的libc然后创建的线程将具有不同的PID,如下所示

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

并且我尝试在包含ubuntu libc6(2)中的libc的linux上运行该程序,即,较新版本的libc,然后创建的线程将具有与该进程相同的PID.

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

并且libc(2)使用pthread的NPTL(本地posix线程库")实现

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

根据linuxthreads常见问题解答(在J.3回答中):

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

每个线程实际上是一个具有不同PID的不同进程,并且发送到线程PID的信号只能由该线程处理

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

因此在使用linuxthreads实现的旧版libc中,每个线程都有其不同的PID

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

在使用NPTL实现的新libc版本中,所有线程都具有与主进程相同的PID.

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

NPTL是由redhat团队开发的.并根据 redhat NPTL文档:解决的问题之一NPTL的实现是:

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

这解释了这个问题.

我正在使用新的libc版本,其中包含pthread的NPTL(本地posix线程库")实现.

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

这篇关于线程ID和进程ID之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆