美文网首页
Linux中的pid tid thread_id

Linux中的pid tid thread_id

作者: IvanGuan | 来源:发表于2019-01-02 18:17 被阅读0次

     你是不是也被pid,tid,thread_id 这三个东西弄晕了。每个进程都有数自己的pid,可以用getpid()得到的。同样在我们调用pread_create的系统也线程分配了thread_id,可以使用pthread_self()得到。但是除了这两个id还有一个id比较特殊即线程的PID。在内核中,每个线程都有自己的PID,要得到线程的PID,必须用syscall(SYS_gettid);

    • 实验1
    #include <stdio.h>
    #include <pthread.h>
    #include <sys/types.h>
    #include <sys/syscall.h>
    
    struct message
    {
        int i;
        int j;
    };
    
    void *hello(struct message *str)
    {
        printf("child, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
        printf("the arg.i is %d, arg.j is %d\n",str->i,str->j);
        printf("child, getpid()=%d\n",getpid());
        while(1);
    }
    
    int main(int argc, char *argv[])
    {
        struct message test;
        pthread_t thread_id;
        test.i=10;
        test.j=20;
        pthread_create(&thread_id,NULL,hello,&test);
        printf("parent, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
        printf("parent, getpid()=%d\n",getpid());
        pthread_join(thread_id,NULL);
        return 0;
    }
    
    gcc pid_tid.c -lpthread
    
    ./a.out
    parent, the tid=140073681307456, pid=17540
    parent, getpid()=17540
    child, the tid=140073672972032, pid=17541
    the arg.i is 10, arg.j is 20
    child, getpid()=17540
    
    ps -ef |grep a.out
    root     17540 10972 97 18:06 pts/1    00:01:28 ./a.out
    
    top -H -p 17540
    
    KiB Swap:  9727996 total,  9727996 free,        0 used.  6527404 avail Mem
    PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    17541 root      20   0   14656    380    296 R 93.8  0.0   0:28.38 a.out
    17540 root      20   0   14656    380    296 S  0.0  0.0   0:00.00 a.out
    

    从上例中看出为父进程和子线程的pid均为17540,而子线程的内核pid为17541

     pthread_self函数获取的是线程ID,线程ID在某进程中是唯一的,在不同的进程中创建的线程可能出现ID值相同的情况

    • 实验2
    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    void *thread_one()
    {
        printf("thread_one:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
    }
    
    void *thread_two()
    {
        printf("thread two:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
    }
    
    int main(int argc, char *argv[])
    {
        pid_t pid;
        pthread_t tid_one,tid_two;
        if((pid=fork())==-1)
        {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        else if(pid==0)
        {
            pthread_create(&tid_one,NULL,(void *)thread_one,NULL);
            pthread_join(tid_one,NULL);
        }
        else
        {
            pthread_create(&tid_two,NULL,(void *)thread_two,NULL);
            pthread_join(tid_two,NULL);
        }
        wait(NULL);
        return 0;
    }
    
    [root@xt2 thread]# gcc pid_tid2.c -lpthread
    [root@xt2 thread]# ./a.out
    thread two:int 18674 main process, the tid=139839637239552,pid=18676
    thread_one:int 18675 main process, the tid=139839637239552,pid=18677
    
    

    参考文章

    <1> https://www.cnblogs.com/lakeone/p/3789117.html

    相关文章

      网友评论

          本文标题:Linux中的pid tid thread_id

          本文链接:https://www.haomeiwen.com/subject/mxctrqtx.html