Search code examples
pthreadsubuntu-12.04nptllightweight-processes

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.

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

#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

$ ./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

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

If I kill the pid 2108, the process will be killed

$ kill -9 2108

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

And if I run the program through gdb, I can see 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. The Above seems that NPTL is still using LWP under. Am I right? I want to know the truth about NTPL and LWP.

Thanks.


Solution

  • The two threads do share one PID, as shown by your first example.

    htop is showing you TIDs (Thread IDs) in the field marked as PID.