简体   繁体   English

为什么NPTL中的两个线程在Ubuntu12.04中具有不同的pid

[英]Why two threads in NPTL have different pid in Ubuntu12.04

I tested some code in Ubuntu 12.04 LTS server x64(3.2 kernel), which I think is using NPTL. 我在Ubuntu 12.04 LTS服务器x64(3.2内核)中测试了一些代码,我认为它使用的是NPTL。

when I run 当我跑步时

$ getconf GNU_LIBPTHREAD_VERSION

I get 我懂了

NPTL 2.15

The following is the test code. 以下是测试代码。 I compiled it with gcc -g -Wall -pthread 我用gcc -g -Wall -pthread编译了它

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>

void *func1(void *arg)
{
    printf("func1:[%d]%lu\n", getpid(), pthread_self());
    for(;;);
    return (void *)0;
}

int main(void)
{
    printf("main:[%d]%lu\n", getpid(), pthread_self());

    pthread_t tid1;
    pthread_create(&tid1, NULL, func1, NULL);
    void *tret = NULL;
    pthread_join(tid1, &tret);

    return 0;
}

when I run the program, it seems all to be expected: two threads have the same pid 当我运行程序时,似乎所有的事情都是预期的: 两个线程具有相同的pid

$ ./a.out
main:[2107]139745753233152
func1:[2107]139745744897792

but in htop (a top like tool, you can get it by apt-get) I see this: two threads have different pid 但是在htop中(一种顶级工具,您可以通过apt-get来获得它)我看到了: 两个线程具有不同的pid

PID  Command
2108 ./a.out
2107 ./a.out

If I kill the pid 2108 , the process will be killed 如果我杀死了pid 2108 ,进程将被杀死

$ kill -9 2108

$./a.out
main:[2107]139745753233152
func1:[2107]139745744897792
Killed

And if I run the program through gdb, I can see LWP 如果我通过gdb运行程序,我可以看到LWP

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
main:[2183]140737354069760
[New Thread 0x7ffff77fd700 (LWP 2186)]
func1:[2183]140737345738496

I think NPTL's threads share one PID and LWP is for LinuxThreads before kernel 2.6. 我认为NPTL的线程共享一个PID,而LWP适用于内核2.6之前的LinuxThreads。 The Above seems that NPTL is still using LWP under. 以上看来,NPTL仍在使用LWP下。 Am I right? 我对吗? I want to know the truth about NTPL and LWP. 我想知道有关NTPL和LWP的真相。

Thanks. 谢谢。

The two threads do share one PID, as shown by your first example. 如第一个示例所示,这两个线程确实共享一个PID。

htop is showing you TIDs (Thread IDs) in the field marked as PID. htop在标记为PID的字段中显示TID(线程ID)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM