繁体   English   中英

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

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

我在Ubuntu 12.04 LTS服务器x64(3.2内核)中测试了一些代码,我认为它使用的是NPTL。

当我跑步时

$ getconf GNU_LIBPTHREAD_VERSION

我懂了

NPTL 2.15

以下是测试代码。 我用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;
}

当我运行程序时,似乎所有的事情都是预期的: 两个线程具有相同的pid

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

但是在htop中(一种顶级工具,您可以通过apt-get来获得它)我看到了: 两个线程具有不同的pid

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

如果我杀死了pid 2108 ,进程将被杀死

$ kill -9 2108

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

如果我通过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

我认为NPTL的线程共享一个PID,而LWP适用于内核2.6之前的LinuxThreads。 以上看来,NPTL仍在使用LWP下。 我对吗? 我想知道有关NTPL和LWP的真相。

谢谢。

如第一个示例所示,这两个线程确实共享一个PID。

htop在标记为PID的字段中显示TID(线程ID)。

暂无
暂无

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

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