Posix线程ID与linux线程ID是否有一对一的关系?

问题描述:

我知道pthread_self()syscall(SYS_gettid)之间的differencepthread_create()产生一个POSIX线程ID,它由一个struct pthread_t表示,它通常被定义为一个unsigned long int。我们可以使用pthread_self来获得pthread_create生成的ID of the threadPosix线程ID与linux线程ID是否有一对一的关系?

随着strace,我知道pthread_create()在libpthread.so.0是通过调用clone系统调用,这也是用于fork()系统调用来实现。通过调用pthread_create()创建POSIX线程后,会生成一个新的POSXI线程(由pthread_self()返回的线程ID标识)和一个新的Linux线程(由线程标识由syscall(SYS_gettid)返回)。这是否意味着POSIX线程ID与linux线程ID具有一对一的关系?它们分别代表pthread_tpid_t的线程?

实际上,有时我发现一个linux线程ID在同一个进程中映射到多个POSIX线程ID,这意味着在通过调用pthread_create()产生一对POSIX线程ID和linux线程ID后,POSIX线程ID改变linux线程ID保持不变。有没有办法改变POSIX线程ID,同时保持linux线程ID不变?如果有,那pthread函数是什么?

谢谢。

这里是通过拦截forkpthread_create调用的日志。 ltid表示linux线程ID,tid表示POSIX线程ID,pid表示进程ID。

1 message: fork pid:12832 ltid:12832 tid:140300035462976  child pid:12848 ltid:12848 tid:140300035462976 

2 message: fork pid:12848 ltid:12848 tid:140549640255296  child pid:12849 ltid:12849 tid:140549640255296 

3 message: fork pid:12848 ltid:12848 tid:140549640255296  child pid:12850 ltid:12850 tid:140549640255296 

4 message: fork pid:12848 ltid:12848 tid:140549640255296  child pid:12851 ltid:12851 tid:140549640255296 

5 message: pthread_create pid:12848 ltid:12848 tid:139968995022656  child ltid:12865 tid:139968995018496 

6 message: pthread_create pid:12848 ltid:12865 tid:139968995018496  child ltid:12865 tid:139968933345024 

7 message: fork pid:12832 ltid:12832 tid:140300035462976  child pid:12885 ltid:12885 tid:140300035462976 

8 message: fork pid:12885 ltid:12885 tid:139870512949056  child pid:12886 ltid:12886 tid:139870512949056 

我的解释:

  1. (PID = 12832,程序ltid = 12832,TID = 140 ... 976)调用fork生产(PID = 12848,程序ltid = 12848,TID = 140 .. 0.976)
  2. (PID = 12848,的ltid = 12848,TID = 140 ...... 296)调用fork产生(PID = 12849,的ltid = 12849,TID = 140 ...... 296)
  3. (PID = 12848 ,ltid = 12848,tid = 139 ... 656)调用pthread_create生成(ltid = 12865,tid = 139 ... 496)

2的调用者是根据linux线程ID(12848)的1的结果,但它们具有不同的POSIX线程ID。 15也是如此。

这是拦截代码片段。

void *intermedia(void * arg){ 

    struct thread_param *temp; 

    void *(*start_routine) (void *); 
    temp=(struct thread_param *)arg; 

    char test[1024]=""; 
    sprintf(test,"child ltid:%ld\ttid:%lu\n",syscall(SYS_gettid),pthread_self()); 
    log_message(test) 

    return temp->start_routine(temp->args);  
} 


int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg){ 
    static void *handle = NULL; 
    static P_CREATE old_create=NULL; 
    if(!handle) 
    { 
     handle = dlopen("libpthread.so.0", RTLD_LAZY); 
     old_create = (P_CREATE)dlsym(handle, "pthread_create"); 
    } 
    pthread_t tmp=pthread_self(); 
    char test[1024]=""; 
    sprintf(test,"pthread_create pid:%d\tptid:%ld\ttid:%lu\n",getpid(),syscall(SYS_gettid),tmp); 
    log_message(test); 

    struct thread_param *temp=malloc(sizeof(struct thread_param)); 
    temp->args=arg; 
    temp->start_routine=start_routine; 

    int result=old_create(thread,attr,intermedia,(void *)temp); 

    return result; 
} 

pid_t fork(void){ 
    static void *handle = NULL; 
    static FORK old_fork=NULL; 
    if(!handle) 
    { 
     handle = dlopen("libc.so.6", RTLD_LAZY); 
     old_fork = (FORK)dlsym(handle, "fork"); 
    } 

    char test[1024]=""; 
    sprintf(test,"fork pid:%d\tltid:%ld\ttid:%lu\t",getpid(),syscall(SYS_gettid),pthread_self()); 

    pid_t ppid=getpid(); 
    pthread_t ptid=pthread_self(); 
    pid_t result=old_fork(); 
    if(result==0){ 
     sprintf(test,"%s\tchild pid:%d\tltid:%ld\ttid:%lu\n",test,getpid(),syscall(SYS_gettid),pthread_self()); 
     log_message(test); 
    } 
    return result; 
} 
+0

标记“linuxthreads”在上下文中似乎是错误的,因为“linuxthreads”与POSIX线程不同。 – alk

+0

@alk这是否意味着Linux内核线程?如果不合适,我可以删除这个标签。 – Chalex

+0

关于在Linux上进行线程化的历史:http://www.drdobbs.com/open-source/nptl-the-new-implementation-of-threads-f/184406204(阅读可能会使不同的字眼清晰) – alk

不POSIX线程ID与Linux的线程ID

是一个一比一的关系。

但请考虑这个实现细节。其他操作系统可能会做这个不同。


其通常被定义为

pthread_t是不透明的。也不要对它的实现做任何假设。


我发现有一个Linux的线程ID映射到多个POSIX线程ID

真的吗?我怀疑这一点。至少不是所有POSIX线程ID都是有效的,也就是说相关线程还没有被加入,或者如果运行分离,线程还没有结束。

+0

他们在我的测试案例中都是有效的。因为我拦截了所有的'pthread_create()'调用来记录线程之间的父子关系。 – Chalex

+0

@Chalex:敢于展示测试用例的代码? – alk

+0

它们在我的测试用例中都是有效的。因为我拦截了所有的'pthread_create()'调用来记录线程之间的父子关系。我发现线程具有相同的linux线程标识,但是不同的POSIX线程标识全部调用'pthread_create()',这意味着它们被执行。 – Chalex