Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Posix线程ID与linux线程ID是否存在一对一关系?_C_Linux_Multithreading_Pthreads_Linuxthreads - Fatal编程技术网

C Posix线程ID与linux线程ID是否存在一对一关系?

C Posix线程ID与linux线程ID是否存在一对一关系?,c,linux,multithreading,pthreads,linuxthreads,C,Linux,Multithreading,Pthreads,Linuxthreads,我知道pthread\u self()和syscall(SYS\u getid)之间的关系pthread_create()生成一个POSIX线程ID,该ID由结构pthread_t表示,该结构通常定义为无符号长int。我们可以使用pthread\u self获取由pthread\u create生成的 通过strace,我知道libpthread.so.0中的pthread\u create()是通过调用clone系统调用来实现的,这也是用于fork()的系统调用。通过调用pthread\u c

我知道
pthread\u self()
syscall(SYS\u getid)
之间的关系
pthread_create()
生成一个POSIX线程ID,该ID由结构pthread_t表示,该结构通常定义为
无符号长int
。我们可以使用
pthread\u self
获取由
pthread\u create
生成的

通过
strace
,我知道libpthread.so.0中的
pthread\u create()
是通过调用
clone
系统调用来实现的,这也是用于
fork()
的系统调用。通过调用
pthread\u create()
创建POSIX线程后,将生成一个新的POSXI线程(由
pthread\u self()
返回的线程ID标识)和一个新的linux线程(由
syscall(SYS\u getId)
返回的线程ID标识)。这是否意味着POSIX线程ID与linux线程ID有一对一的关系?它们只是分别用
pthread\u t
pid\u t
表示线程

实际上,有时我发现一个linux线程ID映射到同一进程中的多个POSIX线程ID,这意味着通过调用
pthread_create()
生成一对POSIX线程ID和linux线程ID后,POSIX线程ID会发生变化,而linux线程ID保持不变。有没有办法在保持linux线程ID不变的情况下更改POSIX线程ID?如果有,它是哪个
pthread
函数

多谢各位

下面是通过拦截
fork
pthread\u 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
    producting(pid=12848,ltid=12848,tid=140…976)
  • (pid=12848,ltid=12848,tid=140…296)调用
    fork
    producting(pid=12849,ltid=12849,tid=140…296)
  • (pid=12848,ltid=12848,tid=139…656)调用
    pthread_create
    producting(ltid=12865,tid=139…496)
  • 2
    的调用方是根据linux线程ID(12848)的
    1
    的结果,但它们具有不同的POSIX线程ID。这同样适用于
    1
    5

    这是截取代码片段

    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;
    }
    
    Posix线程ID与linux线程ID是否存在一对一关系

    但请考虑这是一个实现细节。其他OSs可能会有不同的做法


    通常被定义为

    pthread\u t
    是不透明的。同样,不要对如何实施它做出任何假设


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


    真的吗?我对此表示怀疑。至少在所有相关POSIX线程ID都有效的情况下是这样,即相关线程尚未加入,或者,如果运行分离,线程尚未结束。

    标记“linuxthreads”在上下文中似乎是错误的,因为“linuxthreads”与POSIX线程不同。@alk这是否意味着linux内核线程?如果不合适,我可以删除这个标签。在Linux上线程历史记录上:(阅读可能会使不同的措辞更加清晰)它们在我的测试用例中都是有效的。因为我截取了所有
    pthread\u create()
    调用来记录线程之间的父子关系。它们在我的测试用例中都是有效的。因为我截取了所有
    pthread\u create()
    调用来记录线程之间的父子关系。我发现具有相同linux线程ID但不同POSIX线程ID的线程都称为
    pthread_create()
    ,这意味着它们已被执行。很抱歉再次发布几乎相同的评论。这是一个错误。我将很快发布记录的日志和相应的代码片段。谢谢。看来
    pthread\u t
    基本上是一个存储地址。因此,如果您的程序恰好具有相同的可用存储地址,则不同的pthread实际上似乎具有相同的值
    pthread\t
    ,如64位系统中的139784211150592。