首页 文章

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

提问于
浏览
0

我知道 pthread_self()syscall(SYS_gettid) 之间的difference . pthread_create() 生成一个POSIX线程ID,它由struct pthread_t表示,通常定义为 unsigned long int . 我们可以使用 pthread_self 来获取 pthread_create 生成的ID of the thread .

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

实际上,有时候我发现一个linux线程ID在同一个进程中映射到几个POSIX线程ID,这意味着在通过调用 pthread_create() 生成一对POSIX线程ID和linux线程ID之后,POSIX线程ID会在linux线程ID保持不变的情况下发生变化相同 . 有没有办法在保持linux线程ID不变的同时更改POSIX线程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

我的解释:

  • (pid = 12832,ltid = 12832,tid = 140 ... 976)调用 fork 产生(pid = 12848,ltid = 12848,tid = 140 ... 976)

  • (pid = 12848,ltid = 12848,tid = 140 ... 296)调用 fork 产生(pid = 12849,ltid = 12849,tid = 140 ... 296)

  • (pid = 12848,ltid = 12848,tid = 139 ... 656)调用 pthread_create 产生(ltid = 12865,tid = 139 ... 496)

根据linux线程ID(12848), 2 的调用者是 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;
}

1 回答

  • 1

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

    是 .

    但请考虑这是一个实现细节 . 其他操作系统可能会以不同方式执


    通常定义为

    pthread_t 是不透明的 . 同样不要对它的实现方式做任何假设 .


    我发现一个linux线程ID映射到几个POSIX线程ID

    真?我怀疑这一点 . 至少不是如果所讨论的所有POSIX线程ID都有效,那么相关线程尚未加入,或者如果运行分离,则线程尚未结束 .

相关问题